Posts: 16
Threads: 7
Joined: Jul 2010
Reputation:
0
07-17-2010, 12:19 AM
Hi, I am looking a function that takes a number from a string.
For example I have:
"The number of players was 86 and..."
Is there any native function of vbScripting that does this?
Thanks
Posts: 64
Threads: 7
Joined: Aug 2008
Reputation:
0
07-19-2010, 10:40 AM
Hello,
Below is the example code to get the number from string.
Code: l_String="The number of players was 86 and..."
l_num=""
For i=1 to len(l_String)
y=mid(l_String,i,1)
If ( isnumeric(y)=True) Then
l_num= l_num & y
End If
Next
msgbox l_num
Posts: 34
Threads: 2
Joined: Sep 2009
Reputation:
0
07-19-2010, 12:32 PM
Code: strString="The number of players was 86 and 88 hkjh 880"
splitString=Split(strString," ")
intCount= UBound(splitString)
intCount= Cint(intCount)
arrayNum=" "
For i = 0 to intCount
If IsNumeric(splitString(i)) Then
arrayNumbers= splitString(i)
If arrayNumbers<>"" and arrayNum =" " Then
arrayNum= arrayNumbers
Else
arrayNum= arrayNum &"_"& arrayNumbers
End If
End If
Next
MsgBox arrayNum
Posts: 16
Threads: 7
Joined: Jul 2010
Reputation:
0
07-21-2010, 12:07 AM
(This post was last modified: 07-21-2010, 01:27 AM by pjeigenn.)
Thanks. Looking the string, it has several numbers, but the number i need is next to the word "código" (code in English), it can be done with regular expressions? Or I have to add some lines to the code below.
I will think the solution, if it works i will post it.
Solved
Code: l_String=Datatable("o_DescRechazo", dtLocalSheet)
l_num=""
For j=1 to len(l_String)
y=mid(l_String,j,6)
If ( y="Código") Then
For i=j to len(l_String)
y=mid(l_String,i,1)
If ( isnumeric(y)=True) Then
l_num= l_num & y
End If
Next
j=len(l_string)
End If
Next
msgbox l_num
Thanks to all
Posts: 1,003
Threads: 1
Joined: Jul 2009
Reputation:
5
07-22-2010, 08:39 AM
I understand it is solved. Just curious to know, If your text string was so,
txtstrng = "this is codeigo 86 testing numeric"
I would simply do this,
Code: reqnum = Split(Split(txtstrng,"codeigo")(1),"testing")(0)
msgbox reqnum
Why run unnecessary loops ?
Food for thought mate...
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Posts: 16
Threads: 7
Joined: Jul 2010
Reputation:
0
07-26-2010, 09:16 PM
(This post was last modified: 07-26-2010, 09:18 PM by pjeigenn.)
It is an interesting solution... the "código" word is always going to be before the number i need, but i don´t know what is next to that word.
Btw, i don´t completely understand what are you trying to do there, why (1)? why (0)?
It is an interesnting alternative, with better performance...
Although it doesn´t work "The subindex is out of range: '[number: 1]'"
Posts: 516
Threads: 17
Joined: Jul 2009
Reputation:
3
07-26-2010, 10:04 PM
Hi,
Let me explain the below in detailed.
Code: txtstrng = "this is codeigo 86 testing numeric"
reqnum = Split(Split(txtstrng,"codeigo")(1),"testing")(0)
Let us look into the inner split first and then the outer split.
When you use split method using "codeigo", then it will breake into 2 strings.
Split(txtstrng,"codeigo")(1):
String(0) = "this is codeigo"
String(1)= "86 testing numeric"
So, Split(txtstrng,"codeigo")(1) = "86 testing numeric"
Now here again we are spliting the string with "testing"
Split(Split(txtstrng,"codeigo")(1),"testing")(0):
Split("86 testing numeric","testing")
When you use split method using "testing", then it will breake into 2 strings.
string(0)= "86 "
string(1)="testing numeric"
As we need the numeric value. We have to capture the first string i.e. string(0).
Please let me know if you still have any questions.
Thanks,
SUpputuri
Posts: 15
Threads: 2
Joined: Nov 2010
Reputation:
0
12-02-2010, 12:40 PM
Hi there,
You can use this way to get any number(s) from a string
Code: Public sub s_GetNumberFromString (strText, byref colNum)
Set ObjRegExp = New RegExp
ObjRegExp.Pattern = "\d+"
ObjRegExp.Global = True
Set colNum = ObjRegExp.Execute(strText)
End Sub
Dim colNum
s_GetNumberFromString strNormal, colNum
For each intNum in colNum
MsgBox intNum
Next
Ngoc Vo
Posts: 57
Threads: 0
Joined: Jan 2010
Reputation:
0
12-02-2010, 01:32 PM
Code: Dim regEx: Set regEx = New RegExp
regEx.Pattern = "(?:código )(\d+)"
MsgBox regEx.Execute("this is código 86 testing numeric").Item(0).SubMatches.Item(0)
(?: ) is a non-capturing group, so you are assured the digits will end up in the first SubMatch of the SubMatches collection. If you want to break the code down a little further:
Code: Dim txtstrng: txtstrng = "this is código 86 testing numeric"
Dim regEx: Set regEx = New RegExp
regEx.Pattern = "(?:código )(\d+)"
Dim regExMatches: Set regExMatches = regEx.Execute(txtstrng)
For i = 0 To regExMatches.Count-1
MsgBox "código match number " & i & ": " & regExMatches.Item(i).SubMatches.Item(0)
Next
This will iterate through each match if there are more than one.
Posts: 146
Threads: 50
Joined: Dec 2008
Reputation:
1
12-03-2010, 04:26 PM
Code: Dim var
var = inputbox("Enter the string with any numeric value", "Alpha Numeric", "Baba205Fakruddin")
Function Get_No_FromString(var)
len_var = len(var)
bln=""
For i=1 to len_var
cha =mid(var,i,1)
If isnumeric(cha) Then '1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9)
bln = bln&cha
print bln
End If
Next
print "The digits in string"&bln
Get_No_FromString=bln
End Function
No_in_String = Get_No_FromString(var)
msgbox "The digits available in string"&No_in_String
|