user5717551 user5717551 - 2 years ago 89 Question

VB .Remove from String

I have to prepare a school project of a Currency Converter in Visual Studio using Basic language.
I get the value of extange from XE website and it returns a string like "1.23454 GBP' So I need to remove most characters and numbers from it to have it as a Decimal, not String anymore so I can times it later in calculation.
I've tried to use .Remove anywhere and place it in textBox or Label but it didn't work.

Public Class receipt
Private Sub receipt_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim testString As String = "sadasdasd"
customername.Text = "Name: " + My.Settings.Username
Label6.Text = "Entered Money: " + My.Settings.inputamount + " " + My.Settings.currency
Label7.Text = "Converted To: " + My.Settings.outputamount
Label8.Text = "Charge: 0 (0%)"
TextBox1.Text = testString.Remove(4, 9)
End Sub

End Class

Screenshot of program running

Can someone help me to get this work?

Answer Source

Given your current input of "sadasdasd" and your expected return value then the call to string.Remove should be

TextBox1.Text = testString.Remove(3)

If you look at the MSDN documentation page of string.Remove you could see that the index is zero based so you want to remove anything starting from the fourth character that is at index 3 (keeps 0,1,2, removes from 3)

Also It seems that you think that the Remove second parameter is the end index where the Remove action should stop. But this is wrong, the second parameter is the number of characters to remove so 9 is not correct and probably gives you an ArgumentOutofRange Exception

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download