By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,333 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,333 IT Pros & Developers. It's quick & easy.

String.Trim

P: n/a
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?

Apr 4 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
jvb
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

Apr 4 '06 #2

P: n/a
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 4 '06 #3

P: n/a
> 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/jwoole...03/15/788.aspx
Apr 4 '06 #4

P: n/a
BK
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)

Apr 4 '06 #5

P: n/a
BK,

All you should need is:

Dim temp As String = Me.txtAmount.Text

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

Kerry Moorman
"BK" wrote:
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)

Apr 4 '06 #6

P: n/a
> 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).ToStrin g)
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
Apr 4 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.