String.Trim

  • Thread starter Thread starter BK
  • Start date Start date
B

BK

Consider the following code:

Dim temp As String = Me.txtAmount.Text
Dim nonNumerics As String
nonNumerics = "$,"
temp = temp.Trim(nonNumerics.ToCharArray)

If txtAmount.Text contains "$1,000.00", shouldn't the
temp.Trim(nonNumerics.ToCharArray) strip out the comman (,)? It strips
out the dollar ($) sign, but not the comma. I need to pull out any
nonnumeric characters from the text so that "$1,000.00" becomes
"1000.00", any other ideas?
 
If txtAmount.Text contains "$1,000.00", shouldn't the
temp.Trim(nonNumerics.ToCharArray) strip out the comman (,)?

No, it only removes characters at the beginning and end of the string.

I need to pull out any
nonnumeric characters from the text so that "$1,000.00" becomes
"1000.00", any other ideas?

You can try using Stirng.Replace instead.


Mattias
 
I tried this and it appears for some reason that commas aren't removed
using this method. You can cast that string representation directly
using CDbl. Try this:

Public Sub Main()

Dim str1 As [String] = "$1,000.01"

Console.WriteLine("Original string: {0}", str1)
Console.WriteLine("Cast string: {0}", CDbl(str1))

Console.Write("Press any key to continue...")
Console.ReadLine()

End Sub
 
Consider the following code:
Dim temp As String = Me.txtAmount.Text
Dim nonNumerics As String
nonNumerics = "$,"
temp = temp.Trim(nonNumerics.ToCharArray)
If txtAmount.Text contains "$1,000.00", shouldn't the
temp.Trim(nonNumerics.ToCharArray) strip out the comman (,)? It
strips out the dollar ($) sign, but not the comma. I need to pull out
any nonnumeric characters from the text so that "$1,000.00" becomes
"1000.00", any other ideas?

It appears that the key question is, what do you plan to do with temp. If
you are going to work with it as a number, you might want to consider decimal.Parse
or decimal.TryParse. You will need to use the overload that includes the
NumberStyles option if you need to handle currency.

Jim Wooley
http://devauthority.com/blogs/jwooley/archive/2006/03/15/788.aspx
 
Thanks, that did it!

I was trying to return a double, so I it to:

Dim temp As String = Me.txtAmount.Text
Dim nonNumerics As String
nonNumerics = "$,"
temp = temp.Trim(nonNumerics.ToCharArray)
Dim returnValue As Double

Return returnValue.Parse(temp)
 
BK,

All you should need is:

Dim temp As String = Me.txtAmount.Text

Return Double.Parse(temp, Globalization.NumberStyles.Currency)

Kerry Moorman
 
BK,
All you should need is:

Dim temp As String = Me.txtAmount.Text

Return Double.Parse(temp, Globalization.NumberStyles.Currency)

Actually, you would need to wrap this in a try..catch block as Double.Parse
will throw a format exception if the temp value is not a number. As an alternative,
you could use the following:

Sub Main()
Console.WriteLine(GetValue(txtAmount.Text).ToString)
Console.ReadLine()
End Sub

Private Function GetValue(ByVal InVal As String) As Double
Dim temp As Double
If Double.TryParse(InVal, temp) Then
Return temp
Else
'Return something else to indicate that this is an invalid input
Return Double.MinValue
End If
End Function

Jim Wooley
http://devauthority.com/blogs/jwooley
 
Back
Top