Cancel
Showing results for 
Search instead for 
Did you mean: 

Split String

Valued Contributor
Valued Contributor

Hi,

 

I have some trouble with the splitString method.

 

    local command: string := "Test[split]Test2";
    local array := splitString(command,"[split]");
    print array;
    for local i := 1 to array.dim loop
        print array[i];
    next;

when i runn this the output is

 

[Te, Te, 2]
Te
Te
2

but i want

[Test, Test2]
Test
Test2

 

 

3 REPLIES

Re: Split String

Phenom
Phenom

FYI

 

local command: string := "Test,Test2";


local array := splitString(command,",");
	
	
    for local i := 1 to array.dim loop
        print array[i];
    next;

Re: Split String

Valued Contributor
Valued Contributor

i didn't want so use any common characters like "," but i solved it by using chr(7) to separate the input. I also wrote a replace method to replace the strings like this

 

(input:string;pattern:string) :string
is
do    
    local newStr :string;
    for local index := 1 to strlen(input) loop
        local check :boolean := true;
        if index <= strlen(input)-strlen(pattern) then
            for local index2 := 1 to strlen(pattern) loop
                local sum :integer := (index+index2)-1;
                if chr(ascii(input,sum)) /= chr(ascii(pattern,index2)) then
                    check := false;
                end;
            next;
        else
            check := false;
        end;
        if check then
            index := index + strlen(pattern)-1;
            newStr := newStr + chr(7);
        else
            newStr := newStr + chr(ascii(input,index));
        end;
    next;    
    return newStr;
end;

 so now i can use

    local command: string := "Test[split]Test2";
    command := replaceString(command,"[split]");
    local array := splitString(command,chr(7));
    print array;

to replace all strings in a string.

I dont think that this is the best way to do it but it works ...

Re: Split String

Phenom
Phenom

it seems splitString expects a single character separator

 

in this case probably the best way is using the regular expression replace function (v12.2, s.b.)

 

local command: string := "Test[split]Test2";
command := regex_replace(command,"\[split\]",chr(7)) ;
local array := splitString(command,chr(7));
print array;