Computer Science Canada

Need help with string manipulation

Author:  yingmu [ Sun Apr 10, 2005 7:59 pm ]
Post subject:  Need help with string manipulation

ok u have a string, and the beginning part was a number followed by words with no space in between, how do u get the number into interger, ex. if the string was "123abc" how do u get the number 123? plz help, thx Very Happy [/b]

Author:  jamonathin [ Sun Apr 10, 2005 8:08 pm ]
Post subject: 

This is what I got, but there's some errors, or attributes, depends on how you see it. It'll take the number out, but it will put the number in order.
code:
var number : int := 0
var hold : string := ""
var chars : string := "123abc"
for i : 0 .. 9
    for q : 1 .. length (chars)
        if chars (q) = intstr (i) then
            hold += chars (q)
        end if
    end for
end for
number := strint (hold)
put number

use "1231abc" and see for yourself. I can't think of anything else right now, but i figured this was better than nothing. Confused

Author:  jamonathin [ Sun Apr 10, 2005 8:21 pm ]
Post subject: 

Here yas go. I did it backwards, here's the final good copy.
code:
var number : int := 0
var hold : string := ""
var chars : string := "423ab1c"
for i : 1 .. length (chars) %Checking Every Character
    for q : 0 .. 9 %Checking If There's a Number There
        if chars (i) = intstr (q) then
            hold += chars (i) %Adding the Number to a String
        end if
    end for
end for
number := strint (hold) %Converting the String to an Int
put number

What you do is check every character in the string to see if there's a number, if there is, add it to a variable that can hold the information, then transfer the information to an integer variable.

Author:  McKenzie [ Sun Apr 10, 2005 8:30 pm ]
Post subject: 

yingmu,

Unless you are trying to go above and beyond I've said that you only need to worry about one digit numbers.

Author:  jamonathin [ Sun Apr 10, 2005 8:34 pm ]
Post subject: 

McKenzie wrote:
yingmu,

Unless you are trying to go above and beyond I've said that you only need to worry about one digit numbers.

why should he only stick to 1-digit?


: