pick numbers from a string

bpnlsu

Member
Joined
Sep 23, 2006
Messages
8
Programming Experience
Beginner
Hi Friends,

Suppose, if string is
Dim str as String = "2 hand 10 fingers 1 head"

I would like to print the sentence as "0.0#2 hand 0.01#0 finger 0.0#1 head"

ie each number in string should be replaced as 0.0#number

Can anybody help me with this... ?

Thanks!
Basky
 
Use String.Split to create a string array containing the individual words. Test each word to see if it is a number using Integer.TryParse. If a string is a number then make the replacement. Convert the array back to a single string using String.Join.
 
This is the code i wrote as u said...
Dim contents As String = "head,1,2,hands,5,fingers,9,stars"
Dim abc As Array
abc = contents.Split(
",")
Dim z As Integer
For z = 0 To UBound(abc) - 1
If Integer.TryParse(abc()) = True Then
Dim test As String = abc(z)
test =
String.Concat("0.0#", test)
abc(z) = test
End If
Next
contents = String.Join(",", abc())
console.write(contents)

It shows on the line "integer.Trypars(abc())" as Error 1 Overload resolution failed because no accessible 'TryParse' accepts this number of arguments.

what would be the problem.... plz help me...

 
Maybe you should try to test a single array element 'abc(z)' instead of the whole array 'abc()'?
Also, do shorten the three lines in loop to:
VB.NET:
abc(z) = [SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Concat([/SIZE][SIZE=2][COLOR=#800000]"0.0#"[/COLOR][/SIZE][SIZE=2], abc(z))[/SIZE]
 
The problem would be exactly waht the error message says. You can't just pass whatever you feel like to a method. As you're typing Intellisense tells you EXACTLY what the method requires. ALWAYS take notice of Intellisense. Also, if a method doesn't work the way you expect, the next thing after consulting Intellisense is to read the help topic for that method. Most will include a code example as well as an explanation.

Integer.TryParse requires you to pass an Integer object as a second parameter, which is where the number is stored if the operation is successful. If you don't want touse that value then just ignore it, but you still have to pass it in in the first place.

Put the cursor just after the "TryParse(" and start hitting Backspace and see what Intellisense tells you.
 

Dim abc As Array
abc = contents.Split(
",")
Dim z As Integer
For z = 0 To UBound(abc) - 1
If Integer.TryParse(abc(z)) = True Then
' i changed here as integer.tryparse(abc(z)) instead of abc()..
abc(z) = String.Concat("0.0#", abc(z))
Else
End If
Next
contents = String.Join(",", abc())

Though i changed like above, it shows the same error....

Plz help me and thank u so much for the previous replies...
 
Hi... thank u so much for ur help.... now it works fine.. after changing like this...
Dim contents AsString = "head,1,2,hands,5,fingers,9,stars"
Dim abc As Array
abc = contents.Split(
",")
Dim z As Integer
For z = 0 To UBound(abc) - 1
If Integer.TryParse(abc(z), 1) = True Then
abc(z) = String.Concat("0.0#", abc(z))
Else
End If
Next
contents = String.Join(",", abc)

Now... i wud like to do this operation... suppose contents = "head,1,2,hands,3stars,-7,debts,-15,deduction"

i would like to print it like "head,1,2,hands,3stars,7,debts,15,deduction"

that is, i would like to remove the negative signs from that.... Can you help out....


 
I reached, what i wanted....

finally.. i did that....
Dim contents AsString = "head,1,2,hands,5,fingers,9,stars,-2,debts,-10,subtract"

Dim abc As Array
abc = contents.Split(
",")
Dim z, hold As Integer
For z = 0 To UBound(abc) - 1
If Integer.TryParse(abc(z), 1) = True Then
hold = CInt(abc(z))
If hold > 0 Then
abc(z) = String.Concat("0.0#", abc(z))
Else
hold = Math.Abs(hold)
abc(z) =
CStr(hold)
End If
Else
End If
Next
contents = String.Join(",", abc)
Console.Write(contents)

Thanks for ur help
 
*sigh*
VB.NET:
Dim dummyInteger As Integer

If Integer.TryParse(myString, dummyInteger) Then
After that code executes dummyInteger contains the parsed value. You're ignoring it, which is why I named it dummyInteger, but if you actually wanted the value that's where you'd get it.
 
Actually, I just read your code and realised that you're not ignoring it. You've just parsed the string to an Integer and then you're converting it again. No, no, no! Did you read the help topic as I suggested? I'm guessing not. As I said, you declare an Integer variable and pass it to Integer.Parse. If the string was successfully parsed the result is then stored in that Integer for you to retrieve.
 
I have changed accordingly now, as u said...


VB.NET:
Dim abc As Array
abc = contents.Split(",")
Dim z As Integer
For z = 0 To UBound(abc) - 1
Dim dummy As Integer
If Integer.TryParse(abc(z), dummy) = True Then
 
If dummy > 0 Then
abc(z) = String.Concat("0.0", abc(z))
Else
dummy = Math.Abs(dummy)
abc(z) = CStr(dummy)
End If
Else
End If
Next
contents = String.Join(",", abc())
 
console.write(contents)
Thanks for ur valuble help....
 
Last edited by a moderator:
You can do all this in one line of code:

VB.NET:
Dim result as String = System.Text.RegularExpressions.Regex.Replace([B]input[/B], "(\\d+)", "0.0#$1")

Though I appreciate why you would do it the hard way
 
Hi,

I am interested to know, how can i change my code to the one which u said...
Dim result as String = System.Text.RegularExpressions.Regex.Replace(input, "(\\d+)", "0.0#$1")

As a beginner, i couldnt understand how to make it....

Dim abc As Array
abc = contents.Split(",")
Dim z As Integer
For z = 0 To UBound(abc) - 1
Dim dummy As Integer
If Integer.TryParse(abc(z), dummy) = True Then

If dummy > 0 Then
abc(z) = String.Concat("0.0", abc(z))
Else
dummy = Math.Abs(dummy)
abc(z) = CStr(dummy)
End If
Else
End If
Next
contents = String.Join(",", abc())

console.write(contents)​
 
Back
Top