473,703 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8241
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).GetT ype.Name.ToStri ng)

' For 50001P
Dim num2 As String = "50001P"
num2 = num2.Substring( 0, 5)
MsgBox(num2 & " is " & _
CInt(num2).GetT ype.Name.ToStri ng)

'.........

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(str X,1))="p" then
dblvalue = val(left(strX,L en(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 ", StringCompariso n.CurrentCultur eIgnoreCase) Then
s = s.Substring(1)
ElseIf s.EndsWith("P", StringCompariso n.CurrentCultur eIgnoreCase) Then
s = s.Substring(0, s.Length - 1)
End If

value = CInt(s)


"Lorenz Hölscher" <in******@softw are-dozent.dewrote in message
news:e4******** *************** ***********@p25 g2000hsf.google groups.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(str X,1))="p" then
dblvalue = val(left(strX,L en(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******** ******@TK2MSFTN GP03.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".tolow er.Trim("p")

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

Cor

"engteng" <pa*****@gmail. comschreef in bericht
news:eH******** ******@TK2MSFTN GP03.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".tolow er.Trim("p")

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

Cor

"engteng" <pass...@gmail. comschreef in berichtnews:eH* *************@T K2MSFTNGP03.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

"kimiraikko nen" <ki************ *@gmail.comschr eef in bericht
news:10******** *************** ***********@x41 g2000hsb.google groups.com...
On Sep 29, 2:06 pm, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
wrote:
Tee,

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

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

Cor

"engteng" <pass...@gmail. comschreef in
berichtnews:eH* *************@T K2MSFTNGP03.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.R eplace(SomeValu e.ToLower, ""))

"engteng" <pa*****@gmail. comwrote in message
news:eH******** ******@TK2MSFTN GP03.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
12168
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 << value << std::endl ; strbuf.flush(); DestinationType convalue; strbuf >> convalue;
4
9757
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 numeric value according to an arbitrary regular expression.
7
13083
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 a negative value. For example if I'm using the xd -c (Unix) on the file, I can see the value FFFFFFFFFFFFFFA2 which using the memcpy as described above I get -94. But the real value that I'd expect to get is a positive one.
3
10284
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 and there you have it. So I enquired and found the Convert class with it's promising ToInt32 method, great... but it doesn't work. The thing keeps throwing Format Exceptions all over the place. What is the "C#" way to do this??? code int wmin,...
4
17681
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 to do a generic cast or conversion on the type? Here is sort of what I want to do: object MyFunc(Type T, String Str) { object o;
2
2665
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
1463
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 integer that was entered. So, if 353 was entered, the output should be 11.
4
25965
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
118816
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 character into a number?????? In Oracle, it is:
0
8670
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9254
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9017
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8967
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5923
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3125
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 we have to send another system
2
2458
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2070
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.