cancel
Showing results for 
Search instead for 
Did you mean: 

UDF mapping to split string

laurent_touillaud
Contributor
0 Kudos

Hi,

I'm starting with UDF functions.

How can i split source string at '-' and take first part into target.

ex : 123-456 -> 123

Easy points for those who know...

Thanks in advance,

Laurent.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Laurent,

Create a <i><b>Value</b></i> user defined function and name it as <b><i>replace</i></b>. Take one argument as <i><b>a</b></i>.

Import: java.*;

Add this code:

int i = a.indexOf("-");

a = a.substring(0,i);

return(a);

Then map like this:

source --> replace(udf) --> Target

This will always take the value before -.

Regards,

---Satish

Answers (5)

Answers (5)

laurent_touillaud
Contributor
0 Kudos

Thanks for all supports

Satish,

thanks it worked immediately.

Nikhil,

I tried your code but there was still error message with array missing.

java:2079: array required, but java.lang.String found String temp = a[0].toString();

Regards,

Laurent

nikhil_bose
Active Contributor
0 Kudos

hi laurent,

<b> I have used Advanced UDF and the code work for the same without errors</b>

regards

nikhil bos

Message was edited by:

Nikhil Bos

nikhil_bose
Active Contributor
0 Kudos

hi laurent,

<b>use advanced udf</b>

int i = 0;
String temp = a[0].toString();
i = temp.indexOf('-');
result.addValue(temp.substring(0,i));

thanks and regards

nikhil bos

<i>**reward if helpful

Former Member
0 Kudos

Hi

use the substring function provided in the text function.

input field->substring-->output value

Thanks

Rinku

Former Member
0 Kudos

Hi Laurent,

Use an Advanced User Defined Function. Adavnced user Defined Functions can return Multiple values.

UDF1:

1.Takes renmark as input.

2.Does the split on the basis of ;

3. Splits each split of step 2 on the basis of /

4. Add the output of step 3 to the resultlist with a context change.

UDF2:

1.Takes renmark as input.

2.Does the split on the basis of ;

3. Splits each split of step 2 on the basis of /

4. Add the output of step 3 to the resultlist with a context change.

For Advanced UDF's,

http://help.sap.com/saphelp_nw04/helpdata/en/f8/2857cbc374da48993c8eb7d3c8c87a/content.htm.

You can try this piece of code in your mapping...

You need 3 UDFs each for <detail>, <field1>,<field2>

UDF1: For <detail>

String arr[] = a[0].split(";");

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

result.addValue(arr);

UDF2: For ><field1>

String str = a[0];

String str1 = null;

String sarr1[] = str.split(";");

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

{

str1 = sarr1;

String sarr2[] = str1.split("/");

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

result.addValue(sarr2[j]);

j++;

}

result.addContextChange();

}

UDF2: For ><field2>

String str = a[0];

String str1 = null;

String sarr1[] = str.split(";");

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

{

str1 = sarr1;

String sarr2[] = str1.split("/");

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

j++;

result.addValue(sarr2[j]);

}

result.addContextChange();

}

..All these functions requires one input <remark>

Hope this helps you ..

*Reward with points if helpful*

Regards,

Shibani

aashish_sinha
Active Contributor
0 Kudos

Hi

use the charAt() function and just do nothing after getting the character "-".ie avoid next characters or break..

regards

Aashish Sinha

PS : reward points if helpful

laurent_touillaud
Contributor
0 Kudos

Hi Aashish,

Could you please provide the code.

I tried with the one below but it doesn't work.

String[] vals = a[0].split("-");

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

{

result.addValue(vals[j]);

}

Regards,

Laurent.

aashish_sinha
Active Contributor
0 Kudos

Hi,

i guess you can use it like this..

String x;String y;

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

{

y = vals.charAt(i);

if ( y == '-')

break;

x = x + vals.charAt(i);

}

result.addValue(x);

you can use it like this with bit of modification with ur code.

Regards

Aashish Sinha

PS : reward points if helpful

laurent_touillaud
Contributor
0 Kudos

Thanks for your reply.

Unfortunately i'm missing something as i'm new to this.

The error says :

array required, but java.lang.String found String[] vals = a[0].split("-");

I pasted the code exactly like yours.

I just have one input string (a) and need one output string.

Regards,

Laurent.

aashish_sinha
Active Contributor
0 Kudos

Hi,

You need to define the input Array also.. then copy paste my program.

<b>The error says :

array required, but java.lang.String found String[] vals = a[0].split("-");</b>

Remove these lines and put an array for input and then use the program exactly as mine.

Regards

Aashish Sinha

PS : reward points if helpful

laurent_touillaud
Contributor
0 Kudos

Hi Aashish,

Sorry again but

How do you define the input array?

Best regards,

Laurent.

aashish_sinha
Active Contributor
0 Kudos

Hi,

public String test(String []vals,Container container)

{

String x;String y;

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

{

y = vals.charAt(i);

if ( y == '-')

break;

x = x + vals.charAt(i);

}

result.addValue(x);

}

Regards

Aashish Sinha

PS : reward points if helpful

laurent_touillaud
Contributor
0 Kudos

Thanks but now i have this error :

java:2077: illegal start of expression public String test(String []vals,Container container) ^ java:2089: ';' expected ^ 2 errors

Regards,

Laurent.