473,321 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Convert string to Numeric in VB.net 2003 ?

How do I convert string to numeric in VB.NET 2003 ?

Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle
then not convert.

Regards,

Tee
Sep 29 '08 #1
9 8204
On Sep 29, 11:03*am, "engteng" <pass...@gmail.comwrote:
How do I convert string to numeric in VB.NET 2003 ?

Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle
then not convert.

Regards,

Tee
In my idea, first you should remove P by taking only numeric part
using substring function, then you're ready to cast it to Integer or
Long using CInt or CLng depending on the range as follows:

' For P50001
Dim num1 As String = "P50001"
num1 = num1.Substring(1)
'Proove that 50001 is now an Int32
MsgBox(num1 & " is " & _
CInt(num1).GetType.Name.ToString)

' For 50001P
Dim num2 As String = "50001P"
num2 = num2.Substring(0, 5)
MsgBox(num2 & " is " & _
CInt(num2).GetType.Name.ToString)

'.........

Hope this helps,

Onur Güzel
Sep 29 '08 #2
Hello Tee,

if the "P" always is the single first or last character you could
write something like:

___________________________________________
dim strX as string
dim dblValue as Short

strX="P50001"
if LCase(Left(strX,1))="p" then
dblvalue = val(mid(strX,2))
elseif LCase(Right(strX,1))="p" then
dblvalue = val(left(strX,Len(strX)-1))
else
dblvalue = 0
end if
___________________________________________

If the character may vary you could change the code into something
like:

___________________________________________
if Not IsNumeric(LCase(Left(strX,1))) then
___________________________________________
(I didn't check it with the debugger so I hope it's free of errors)

Good luck,
Lorenz
Sep 29 '08 #3
Hi Lorenz,

Generally it is better to use a string comparison method than convert
strings to lower case or upper case for the purpose of a case insensitive
match, eg:

If s.StartsWith("P", StringComparison.CurrentCultureIgnoreCase) Then
s = s.Substring(1)
ElseIf s.EndsWith("P", StringComparison.CurrentCultureIgnoreCase) Then
s = s.Substring(0, s.Length - 1)
End If

value = CInt(s)


"Lorenz Hölscher" <in******@software-dozent.dewrote in message
news:e4**********************************@p25g2000 hsf.googlegroups.com...
Hello Tee,

if the "P" always is the single first or last character you could
write something like:

___________________________________________
dim strX as string
dim dblValue as Short

strX="P50001"
if LCase(Left(strX,1))="p" then
dblvalue = val(mid(strX,2))
elseif LCase(Right(strX,1))="p" then
dblvalue = val(left(strX,Len(strX)-1))
else
dblvalue = 0
end if
___________________________________________

If the character may vary you could change the code into something
like:

___________________________________________
if Not IsNumeric(LCase(Left(strX,1))) then
___________________________________________
(I didn't check it with the debugger so I hope it's free of errors)

Good luck,
Lorenz
Sep 29 '08 #4
Hi Tee,

You can try:

value = CInt( s.Trim("P"c, "p"c))
"engteng" <pa*****@gmail.comwrote in message
news:eH**************@TK2MSFTNGP03.phx.gbl...
How do I convert string to numeric in VB.NET 2003 ?

Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle
then not convert.

Regards,

Tee

Sep 29 '08 #5
Hi Bill,

you're right for sure. As you might have seen I usually work with
Office-VBA with no SubString-Method und its IgnoreCase-Parameter until
now...

bye, Lorenz
Sep 29 '08 #6
Tee,

dim Example as string = "P50001p".tolower.Trim("p")

http://msdn.microsoft.com/en-us/library/d4tt83f9.aspx

Cor

"engteng" <pa*****@gmail.comschreef in bericht
news:eH**************@TK2MSFTNGP03.phx.gbl...
How do I convert string to numeric in VB.NET 2003 ?

Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle
then not convert.

Regards,

Tee


Sep 29 '08 #7
On Sep 29, 2:06*pm, "Cor Ligthert [MVP]" <notmyfirstn...@planet.nl>
wrote:
Tee,

dim Example as string = "P50001p".tolower.Trim("p")

http://msdn.microsoft.com/en-us/library/d4tt83f9.aspx

Cor

"engteng" <pass...@gmail.comschreef in berichtnews:eH**************@TK2MSFTNGP03.phx.gbl. ..
How do I convert string to numeric in VB.NET 2003 ?
Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle
then not convert.
Regards,
Tee- Hide quoted text -

- Show quoted text -
Why are you trying to lower P? OP just wants to convert "P50001 to
50001" or "50001P to 50001". Plus, trimming 'P' is not enough alone,
also the string type must be converted to a numeric type such as
Integer for further usage. In this case, it's proper to take the part
of the string value that can be casted to Integer using CInt or
Convert.ToInt32 etc. (without 'P').

Thanks,

Onur G.
Sep 29 '08 #8
Onur,

The first I agree, it is converting the P in the middle as well, to lower.

But how can you convert a string with a P in the middle to a what you call a
numeric?

That can only by those who call a string with all numeric characters a
numeric, and therefore I did not extend the code for that part.

(By the way, I have tried the code, with that what you did, you can do the
same, as you then set a simple Cint before it, you can use it as any real
numeric value instead of a string, however this can in my idea never been
done in the way the Op was asking).

Cor

"kimiraikkonen" <ki*************@gmail.comschreef in bericht
news:10**********************************@x41g2000 hsb.googlegroups.com...
On Sep 29, 2:06 pm, "Cor Ligthert [MVP]" <notmyfirstn...@planet.nl>
wrote:
Tee,

dim Example as string = "P50001p".tolower.Trim("p")

http://msdn.microsoft.com/en-us/library/d4tt83f9.aspx

Cor

"engteng" <pass...@gmail.comschreef in
berichtnews:eH**************@TK2MSFTNGP03.phx.gbl. ..
How do I convert string to numeric in VB.NET 2003 ?
Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle
then not convert.
Regards,
Tee- Hide quoted text -

- Show quoted text -
Why are you trying to lower P? OP just wants to convert "P50001 to
50001" or "50001P to 50001". Plus, trimming 'P' is not enough alone,
also the string type must be converted to a numeric type such as
Integer for further usage. In this case, it's proper to take the part
of the string value that can be casted to Integer using CInt or
Convert.ToInt32 etc. (without 'P').

Thanks,

Onur G.

Sep 30 '08 #9
How about using regular expressions
Dim SomeValue As String = "P500P01"
Dim SomeNumber As Integer = 0
Dim objRegEx As New Regex("[p]")
SomeNumber = CInt(objRegEx.Replace(SomeValue.ToLower, ""))

"engteng" <pa*****@gmail.comwrote in message
news:eH**************@TK2MSFTNGP03.phx.gbl...
How do I convert string to numeric in VB.NET 2003 ?

Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle
then not convert.

Regards,

Tee


Sep 30 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Vijai Kalyan | last post by:
I wrote the following function as a curiosity: template<typename SourceType, typename DestinationType> DestinationType NumericCast(const SourceType& value) { std::wstringstream strbuf; strbuf...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
7
by: Golan | last post by:
Hi, I need to convert a Binary value to Decimal. I've been told that the value is an unsigned one. How can I do this? I use memcpy into an unsigned char variable, but when I print the value I got...
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
4
by: Ken Varn | last post by:
I have an unknown numeric Type object passed into a function. I want to run a conversion on a string to convert the string to that Type object and return an object of that type. Is there some way...
2
by: Joergen Bech | last post by:
Is there a function in the .Net 1.1 framework that will take, say, a string containing Scandinavian characters and output the corret HTML entities, such as &aelig; &oslash; &aring; etc.
14
by: Drew | last post by:
Hi All: I know I am missing something easy but I can't find the problem! I have a program which reads an integer as input. The output of the program should be the sum of all the digits in the...
4
by: simonZ | last post by:
Why this don't work: Boolean test; String testValue; testValue="0"; test=System.Convert.ToBoolean(testValue); How can I convert string to boolean?
4
by: dba_222 | last post by:
Dear Experts, Ok, I hate to ask such a seemingly dumb question, but I've already spent far too much time on this. More that I would care to admit. In Sql server, how do I simply change a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.