Cancel
Showing results for 
Search instead for 
Did you mean: 

Extract numbers from a string

Creator
Creator

Hello!

I need to extract the numbers of a string (the name of a method-calling process). The string will typically be "SP12" or "SingleProces52" or "XXXXX###", being X leters and # numbers.

I need to extract only the numbers from the string. Length of the string and position of the first numer will always be variable.

Is there any way of doing this?

Thank you!

2 REPLIES

Re: Extract numbers from a string

Siemens Phenom Siemens Phenom
Siemens Phenom

For example this way:

is
    n : integer;
do
    local s : string := "SingleProc123";

    for local i := 1 to strlen(s) loop
        local a : integer := ascii(s, i);
        if a >= ascii("0") and a <= ascii("9") then
            n := str_to_num(copy(s, i, 9999));
            exitloop;
        end;
    next;

    print n;  -- prints 123
end;

 

_________________________________________________________________
Did you like the answer? Then click the Thumbs Up button.
Did the answer solve your problem? Then accept the answer as solution.

Re: Extract numbers from a string

Creator
Creator

Perfect! Thanks