cancel
Showing results for 
Search instead for 
Did you mean: 

how to Extract characters from a string

former_member207622
Contributor
0 Kudos

Hey ppl

I have a requirement in which I have value coming in my field as a-b-c

I dont know the length of a and b and c

I have to extract them ( i e a, b , c and then pass them into three respective target fields )

a-b-c are separated by hyphen !

please tell me how tyo achieve this

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

StringTokenizer st = new StringTokenizer(input[0],"-");
while (st.hasMoreTokens()) {
         result.addValue(st.nextToken());
     }

choose context while creating the userdefined function and name of the argument is input and use copyValue standard function to map to the respective target field

please see this [http://help.sap.com/saphelp_nw04/helpdata/en/26/d22366565be0449d7b3cc26b1bab10/content.htm] for how to use copyValue function.

for the first target filed specify 0 in the properties of copyValue function (double click on copyvalue function) ,1 for second target filed and 2 for third target field

former_member207622
Contributor
0 Kudos

hey fatima

I am getting this error while executing your UDF

cannot find symbol symbol : constructor StringTokenizer(java.lang.String[],java.lang.String) location: class java.util.StringTokenizer StringTokenizer st = new StringTokenizer(input,"-"); ^

I have added the classes in import but still not sure why

pls help

shweta_walaskar2
Contributor
0 Kudos

Hi Ninad,

The reason is,you are trying to pass an array in StringTokenizer constructor.Use this:

AbstractTrace trace = container.getTrace();

StringTokenizer st ;

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

{

st = new StringTokenizer(a<i>,"-");

while (st.hasMoreTokens()) {

String str1 = st.nextToken();

result.addValue(str1);

String str2 = st.nextToken();

result.addValue(str2);

String str3 = st.nextToken();

result.addValue(str3);

}

}

Please take care that in bolded line,I have passed a array with index i instead of a,I am not sure why after posting this message ,it changes to a

The output queue is exactly what you want.

and then use copyValue as suggested by Fatima.

Kindly let us know if it works.

Thanks.

Regards,

Shweta

Edited by: Shweta Walaskar on Jun 24, 2009 5:09 PM

Edited by: Shweta Walaskar on Jun 24, 2009 5:10 PM

shweta_walaskar2
Contributor
0 Kudos

Hi Ninad,

From the above mentioned code,you get an array with correct values.In place of copyValue function,create three context based UDFs:

Pass an element a with index i of array a in all result.addValue functions below instead of a which is appearing below

UDF1:

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

{

result.addValue(a<i>);

i+=2;

}

UDF2:

for(int i=1;i<a.length;i++)

{

result.addValue(a<i>);

i+=2;

}

UDF3:

for(int i=2;i<a.length;i++)

{

result.addValue(a<i>);

i+=2;

}

Map output of these three UDFs to the target fields.

Hope this helps.

Thanks.

Regards,

Shweta

Edited by: Shweta Walaskar on Jun 30, 2009 11:13 AM

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi,

the problem is that you can't give parameters to the substring function in mapping (at least until PI 7.0).

So you will have to write a UDF, but can use the logic described by Udo.

Regards

Patrick

former_member207622
Contributor
0 Kudos

hi UDO ,

pleae clarify what all parameters will be required for the second INDEXOF you are using and where to use add function ???

Former Member
0 Kudos

Hi,

maybe an easier way is to use the standard java class java.util.StringTokenizer in an UDF.

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

You could pass the hyphen in the constructor and it will produce the tokens for you.

Regards

Patrick

udo_martens
Active Contributor
0 Kudos

Hi Ninad,

from [SAP help: Standard functions|http://help.sap.com/saphelp_nw2004s/helpdata/en/43/c4cdfc334824478090739c04c4a249/content.htm]: "R = first position from position Z at which string Y is found in X and u20131 if Y does not occur at all."

This function gives you back position from Z, but u want position from first character, therefore just add output of this function and output of first funciton (=Z).

Regards,

Udo

Former Member
0 Kudos

Hi,

u can get the part of a string value coming in my field as a-b-c by using a substring function if

u know the length of the a,b and c.

If u donno the length of a ,b and c please use indexof "-" as a parameter.

shweta_walaskar2
Contributor
0 Kudos

Hi Ninad,

Create a UDF having this code:

StringTokenizer st = new StringTokenizer(a,"-");

while (st.hasMoreTokens()) {

return st.nextToken();

}

It will return individual values

Thanks.

Regards,

Shweta

Former Member
0 Kudos

Hi Sane,

When your input is always in the format a-b-c then for the first element to pick 'a' as an output:

input --> udf1 --> output1

For 2nd element map like this:

input --> udf2 --> output2

For 3rd element map like this:

input --> udf3 --> output3

For all the three udfs create a Value user defined function and name it as udf1, udf2 and udf3. Take one argument as a. Import java.;*

Add this code:

UDF1:

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

a = a.substring(0,i);

return(a);

UDF2:

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

i = i + 1;

int k = a.lastIndexOf("-");

a = a.substring(i,k);

return(a);

UDF3:

int k = a.length();

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

i = i +1;

a = a.substring(i,k);

return(a);

This will work as long as the input is coming correctly. IF not then you may need to add if conditions. Please test with all the possible values.

Regards,

---Satish

udo_martens
Active Contributor
0 Kudos

Hi,

should be possible in standard message mapping without udf:

function indexOf with 2 params: give you position of first hyphen.

function indexOf with 3 params: third param is output of first function, you need to add (function add) output plus output of first function to get position of second hyphen.

With that positions you can use funtion substring to pick up the text values.

Regards,

Udo