473,500 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String.Trim

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
6 886
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
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
> 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
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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
4634
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
32
2342
by: Tubs | last post by:
Am i missing something or does the .Net Framework have a quirk in the way methods work on an object. In C++ MFC, if i have a CString and i use the format method, i format the string i am using. ...
11
5324
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
5
17365
by: Pete | last post by:
How do you get rid of leading white spaces in a string in vb.net? From http://www.developmentnow.com/g/38_2005_9_1_0_0/dotnet-languages-vb.ht Posted via DevelopmentNow.com Group...
2
2225
by: Learner | last post by:
Hello, I am trying to store the data entered in a webform in the database. I have few Int and one SmallDateTime filed in my table in SQL Server 2005 database. I have made a storedproc to store the...
10
2695
by: klineb | last post by:
Good Day, I have written and utility to convert our DOS COBOL data files to a SQL Server database. Part of the process requires parsing each line into a sql statement and validting the data to...
22
9693
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I...
1
6038
by: Sankalp | last post by:
Hi, I am using VB 2005. My application has many data bound controls. The connection is stored in the app.config file. I want the application to start with a default connection string and while...
8
2823
by: Kevin Smith | last post by:
Hi, According to the intellisense help, string.Trim() "Removes all occurances or white space characters from the beginning and end of this instance." However, the follow code does not appear...
8
3227
by: Keith Thompson | last post by:
Kevin Smith <no@spam.comwrites: You posted this to microsoft.public.dotnet.languages.csharp, where I presume it's topical. Why on Earth did you redirect followups to comp.lang.c? Anyone...
0
7149
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7021
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7207
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7401
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4619
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3112
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1434
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
686
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.