473,566 Members | 2,785 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.Te xt
Dim nonNumerics As String
nonNumerics = "$,"
temp = temp.Trim(nonNu merics.ToCharAr ray)

If txtAmount.Text contains "$1,000.00" , shouldn't the
temp.Trim(nonNu merics.ToCharAr ray) 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 890
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.WriteLi ne("Original string: {0}", str1)
Console.WriteLi ne("Cast string: {0}", CDbl(str1))

Console.Write(" Press any key to continue...")
Console.ReadLin e()

End Sub

Apr 4 '06 #2
If txtAmount.Text contains "$1,000.00" , shouldn't the
temp.Trim(nonN umerics.ToCharA rray) 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.Te xt
Dim nonNumerics As String
nonNumerics = "$,"
temp = temp.Trim(nonNu merics.ToCharAr ray)
If txtAmount.Text contains "$1,000.00" , shouldn't the
temp.Trim(nonNu merics.ToCharAr ray) 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.TryPars e. 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.Te xt
Dim nonNumerics As String
nonNumerics = "$,"
temp = temp.Trim(nonNu merics.ToCharAr ray)
Dim returnValue As Double

Return returnValue.Par se(temp)

Apr 4 '06 #5
BK,

All you should need is:

Dim temp As String = Me.txtAmount.Te xt

Return Double.Parse(te mp, Globalization.N umberStyles.Cur rency)

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.Te xt
Dim nonNumerics As String
nonNumerics = "$,"
temp = temp.Trim(nonNu merics.ToCharAr ray)
Dim returnValue As Double

Return returnValue.Par se(temp)

Apr 4 '06 #6
> BK,

All you should need is:

Dim temp As String = Me.txtAmount.Te xt

Return Double.Parse(te mp, Globalization.N umberStyles.Cur rency)


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.WriteLi ne(GetValue(txt Amount.Text).To String)
Console.ReadLin e()
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
4646
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 analysis and I'm led to believe (but can't yet quantitatively establish as fact) that the two basic culprits are a lot of calls to: 1.) if(...
32
2355
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. In dotnet it always asks me to pass it the string. Why can't i just say "stringvariable.Format("0.00") and have it know what i mean. Is there a...
11
5335
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 (strIn.Substring(410, 10).Trim = "") Then 'Something processed Else 'Something processed
5
17370
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 http://www.developmentnow.com
2
2233
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 values. My parameters in the database are like this @DealerShipID int, @ReturnDate smallDateTime,
10
2709
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 keep the integrity of the database. We are parsing roughl 81 files and range in size 1 kb to 65 MB files (Average of 400,000 lines in the larger...
22
9719
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 thought I'd change it to the VB ..NET method "String.Trim" but that throws an object exception. Which brings the question: is it compliant to use...
1
6048
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 during the runtime, the user can click on a button and change the connection string without exiting the application. I would really appreciate...
8
2834
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 to modify s. s.Trim('\r'); While the follow code DOES modify s.
8
3243
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 else replying to Kevin Smith's article, please *ignore* the Followup-To header and post only to the csharp group. Thanks.
0
7666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.