on 04-01-2010 1:05 PM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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,
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,
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,
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.