tags:

views:

593

answers:

2

I have sting

Ex: "We prefer questions that can be answered; not just discussed "

now i want to split this string from ";" like We prefer questions that can be answered and not just discussed

is this possible in DXL.

i am learning DXL, so i don't have idea whether we can split or not.

Note : This is not a home work.

A: 

Quick join&split I could come up with. Seams to work okay.

int array_size(Array a){
    int size = 0;
    while( !null(get(a, size, 0) ) )
        size++;
    return size;
}

void array_push_str(Array a, string str){
    int array_index = array_size(a);

    put(a, str, array_index, 0);
}

string array_get_str(Array a, int index){
    return (string get(a, index, 0));
}

string str_join(string joiner, Array str_array){
    Buffer joined = create;
    int array_index = 0;

    joined += "";

    for(array_index = 0; array_index < array_size(str_array); array_index++){
        joined += array_get_str(str_array, array_index);
        if( array_index + 1 < array_size(str_array) )
            joined += joiner;
    }

    return stringOf(joined)
}

Array str_split(string splitter, string str){
    Array tokens = create(1, 1);
    Buffer buf = create;
    int str_index;

    buf = "";

    for(str_index = 0; str_index < length(str); str_index++){
        if( str[str_index:str_index] == splitter ){
            array_push_str(tokens, stringOf(buf));
            buf = "";
        }else{
            buf += str[str_index:str_index];
        }
    }
    array_push_str(tokens, stringOf(buf));

    delete buf;
    return tokens;
}
+1  A: 

If you only split the string once this is how I would do it:

string s = "We prefer questions that can be answered; not just discussed"

string sub = ";"

int offset

int len

if ( findPlainText(s, sub, offset, len, false)) {

/* the reason why I subtract one and add one is to remove the delimiter from the out put.
 First print is to print the prefix and then second is the suffix.*/

print s[0 : offset -1]

print s[offset +1 :]


} else {
// no delimiter found
print "Failed to match"

}

You could also use regular expressions refer to the DXL reference manual. It would be better to use regular expressions if you want to split up the string by multiple delimiters such as str = "this ; is an;example"

tuckster