cancel
Showing results for 
Search instead for 
Did you mean: 

Split a string with character "+" as separator

Former Member
0 Kudos

Dear Experts,

I would like to know how to split a string with character "+" as separator. I tried to use UDF and split/indexof functions without success.

Example:

Input string: abcdefghijk

desired output: an array of string like:

output[1] = abc

output[2] = defg

output[3] = hijk

Do you have some java code sample or idea in order to help me?

Many thanks in advance,

Landry

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Landry,

Check this code or as Patrik mentioned you can use the string tokenizer also

// Here str is the input variable

String[] temp;

String delimiter = "+";

temp = str.split(delimiter);

for(int i =0; i < temp.length ; i++)

return(temp<i>);

Regards

Ramesh

Former Member
0 Kudos

Hi, Thanks for this reply. I tried to implement it in my code:

for (int j=0;j<ZZField.length;j++){

String[] temp;

String delimiter = "+";

temp = ZZField[j].split(delimiter);

for(int i =0; i < temp.length ; i++)

result.addValue(temp);

}

Here is the returned error code:

Exception:[java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0 + ^]

Do you have any idea?

Thanks in advance,

Former Member
0 Kudos

Hi,

if you have problems with the '+' you could replace it first with the standard function replaceString.

Or better use the tokenizer (there it works

Regards

Patrick

Edited by: Patrick Koehnen on Apr 1, 2010 3:36 PM

Former Member
0 Kudos

Hi,

I tried with Tokenizer. Here is the code:

int i = 0;

StringTokenizer st = new StringTokenizer(ZZField[j],"+");

while (st.hasMoreTokens()) {

temp<i> = st.nextToken();

i++;

}

I need to get an array of string (temp) in output in order to apply other code.

Unfortunately this code doesn't run. An ArrayOutOfBound exception is displayed.

Do you have any idea on the issue?

Thanks for your time,

Former Member
0 Kudos

Hi,

your variable i is never used and your while will always run to the end so that temp will always be the last token.

For getting a token at a special index you should better use a for loop.

The ArrayOutOfBound exception may result from your code ZZField[j].

Regards

Patrick

Former Member
0 Kudos

First of all, thanks for your helps.

Now, code it's running fine with using split method.

for (int j=0;j<ZZField.length;j++){
            String zzf = ZZField[j];
            String resultemp = "";
            String [] temp = null;
                  temp = zzf.split("\\+");
                  for (int k=0;k<temp.length;k++)
                  {
                       int index = temp[k].indexOf(" ");
                       if(index!=-1)
                          { 
            	               temp[k] = temp[k].substring(0,index);
                          }
                  }

               for (int l=0;l<temp.length-1;l++){
              		resultemp = resultemp + temp[l] + "+" ;
    			}
              	resultemp = resultemp + temp[temp.length-1];
    			
    			result.addValue(resultemp);

        }//End FOR

Input: "abc defghi jkl mn"

Output will be: "abcfghil"

Thanks.

Landry,

Former Member
0 Kudos

Hi,

you could write an UDF and use the Java Tokenizer (with '+' as delim):

http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html

You could use the index as an input parameter.

Regards

Patrick