473,466 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to get a number out of string

I have a string in form "abcdefg 123,55 ijklmn"

Now I want get only the number 123,55 within the string !!
Is there any function available in VB.NET ??

Thanks
for any help
Nov 20 '05 #1
8 25145
Hi,

Dim s As String = "abcdefg 123,55 ijklmn"

Dim strWord() As String = s.Split(" ".ToCharArray)

Dim strPart As String
For Each strPart In strWord
Trace.WriteLine(strPart)
Next
Ken
-----------------
"Peter Stojkovic" <Pe*************@gmx.net> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
I have a string in form "abcdefg 123,55 ijklmn"

Now I want get only the number 123,55 within the string !!
Is there any function available in VB.NET ??

Thanks
for any help

Nov 20 '05 #2
But sometimes there is a space, sometimes not

Peter
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:OB*************@TK2MSFTNGP09.phx.gbl...
Hi,

Dim s As String = "abcdefg 123,55 ijklmn"

Dim strWord() As String = s.Split(" ".ToCharArray)

Dim strPart As String
For Each strPart In strWord
Trace.WriteLine(strPart)
Next
Ken
-----------------
"Peter Stojkovic" <Pe*************@gmx.net> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
I have a string in form "abcdefg 123,55 ijklmn"

Now I want get only the number 123,55 within the string !!
Is there any function available in VB.NET ??

Thanks
for any help


Nov 20 '05 #3
Peter Stojkovic schrieb:
I have a string in form "abcdefg 123,55 ijklmn"

Now I want get only the number 123,55 within the string !!
Is there any function available in VB.NET ??


I have once written this code based on a sample I found somewhere on the
web in VB6. Maybe you can convert it to .NET:

\\\
Private Function PurgeNumericInput(ByVal strString As String) As Variant
Dim blnHasDecimal As Boolean, i As Integer
Dim s As String
strString = Trim$(strString)
If Len(strString) = 0 Then
Exit Function
End If
For i = 1 To Len(strString)
Select Case Mid$(strString, i, 1)

' Is this character a number?
Case "0" To "9"

' Add it to the string being built.
s = s & Mid$(strString, i, 1)

' The char is a decimal.
Case ".", ","
If Not blnHasDecimal Then
blnHasDecimal = True
s = s & "."
End If
End Select
Next i
PurgeNumericInput = Val(s)
End Function

Private Sub Main()
Call MsgBox( _
PurgeNumericInput( _
"$44E.mbedded letters and spaces 33 a few more " & _
"pieces of garbage .9" _
) _
)
End Sub
///

It's very quick and dirty, BTW.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4
> I have a string in form "abcdefg 123,55 ijklmn"

Now I want get only the number 123,55 within the string !!
Is there any function available in VB.NET ??


If the numbers are always in teh same place you can do a substring argument.

If not you can always test each character for integer conversions (provided
you do not need the comma) and those that fail reject and move on to the
next character.
Nov 20 '05 #5
"Peter Stojkovic" <Pe*************@gmx.net> schrieb
I have a string in form "abcdefg 123,55 ijklmn"

Now I want get only the number 123,55 within the string !!
Is there any function available in VB.NET ??

Is there a general rule for the format of the string? Something like "the
number is always between the 1st and 2nd blank". Have a look at the members
of the String object to find chars and to extract parts of the string.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #6
Hi,

Dim m As System.Text.RegularExpressions.Match
For Each m In System.Text.RegularExpressions.Regex.Matches("abcd efg
123,55 ijklmnabcs123.4", "\d+(\.?\d+)")
Trace.WriteLine(m.Value)
Next

Ken
---------------------
"Peter Stojkovic" <Pe*************@gmx.net> wrote in message
news:e6**************@tk2msftngp13.phx.gbl...
But sometimes there is a space, sometimes not

Peter
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:OB*************@TK2MSFTNGP09.phx.gbl...
Hi,

Dim s As String = "abcdefg 123,55 ijklmn"

Dim strWord() As String = s.Split(" ".ToCharArray)

Dim strPart As String
For Each strPart In strWord
Trace.WriteLine(strPart)
Next
Ken
-----------------
"Peter Stojkovic" <Pe*************@gmx.net> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
>I have a string in form "abcdefg 123,55 ijklmn"
>
> Now I want get only the number 123,55 within the string !!
> Is there any function available in VB.NET ??
>
>
>
> Thanks
> for any help
>
>



Nov 20 '05 #7
Hi Peter,

Almost the same as Ken's first answer however now with more delimiters.

Dim s As String = "abcdefg 123,55 ijklmn"
Dim delimStr As String = " ,.:;#"
Dim delimiter As Char() = delimStr.ToCharArray()
Dim strWord() As String = s.Split(delimiter)
Dim strPart As String
For Each strPart In strWord
Trace.WriteLine(strPart)
Next

I hope this helps also?

Cor
Nov 20 '05 #8
Using regular expressions - put the imports stmt at the top of the
module:
imports System.Text.RegularExpressions
..
In your code:
dim re as new Regex("(?<num>\d+[.,]\d*|d+|[.,]\d+)")
dim m as match = re.match("abcdefg 123,45 ijklmn")
dim numStr as string

numStr=m.value
or
numstr = m.groups(1).value
or
numstr = m.groups("num").value

The Regex matches numbers in the following format:
x.
x.y
.y
x
where x or y are consecutive digits from 0 to 9.
The 3 numStr assignments are several ways to extract the number. The
last uses the name assigned to the matched string which is the first
part of the Regex pattern ;?<num>. This is optional, but as you take
time to learn regular expressions there is great power in assigning
names to matches.

Hope this helps.
Greg
On Wed, 12 May 2004 12:47:45 +0200, "Peter Stojkovic"
<Pe*************@gmx.net> wrote:
I have a string in form "abcdefg 123,55 ijklmn"

Now I want get only the number 123,55 within the string !!
Is there any function available in VB.NET ??

Thanks
for any help


Nov 20 '05 #9

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

Similar topics

5
by: nboutelier | last post by:
Scenario: you enter "foo bar" into a text field... Is it possible through javascript to select/highlight just "foo"? formObject.select() selects all. I need to select only part of the string....
0
by: Gianluca | last post by:
Anyone knows some open source code to parse date and number strings? The parse functions in the .NET framework are buggy and cannot test for the validity of the string without throwing an...
3
by: Tracey | last post by:
I'm learning C++ and was stuck when writing code to convert a negative decimal number string to a hexdecimal number. For example, suppose the function interface is defined as: int atoi( const...
7
by: jack | last post by:
Hello, I need to get the time down the the 0.1 second in a number string. Example 09-06-03 13:45 23.4 Seconds To "0906031345234"
10
by: Niklas Engfelt | last post by:
Does anyone have a nice algorithm/method to increase all digits one step, e.g. 123123 to 234234 and 4444 to 5555 and 999999 to 000000? Best regards Niklas
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
3
by: Jim Tittsler | last post by:
Is there a standard recipe for getting the subversion revision number into my Python-based application each time I package it up with distutils? (Not just the package name, but also a string that...
3
by: ccarter45 | last post by:
I really need help with this. Your tips are greatly appreciated: import java.util.Scanner; public class PhoneNumbers { public static void main(String args){ Scanner input; ...
2
by: Bart Kastermans | last post by:
Summary: can't verify big O claim, how to properly time this? On Jun 15, 2:34 pm, "Terry Reedy" <tjre...@udel.eduwrote: Thanks for the idea. I would expect the separation to lead to somewhat...
9
by: korea saeed | last post by:
how can i convert number string to number?(without using parsint)
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...
0
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
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
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...
0
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 ...

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.