cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic substring

Former Member
0 Kudos

Hello everybody,

is there an opportunity to use the substring method of message mapping with dynamic parameters and without using a UDF?

Because my requirement is that in an input string there is a semicolon, but not always on the same position. I need now the substring from the beginning to the position of the semicolon. Do you have any ideas how to realize this requirement?

Thanks a lot.

Best Regards

Martin

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Martin,

First get the index of ';' in the string using indexOf text function in graphical mapping.

for indexOf function pass String for first parameter and a constant ";" as second parameter.

Then write a UDF with just a return statement

Pass the String as var1 and the out put of indexOf[;] as var2

 

public String subStr(String var1, int var2, Container container) throws StreamTransformationException
{ 

return var1.substring(0,var2); 

} 

you will get the substring from the begining of the string up to semicolon as the output.

hope this satisfies you requirement.

Regards,

Aravind

Former Member
0 Kudos

Thanks for your advices.... With UDF it works right now.

Best Regards

Martin

Answers (3)

Answers (3)

Former Member
0 Kudos

This works nicely, but its surprising to me that a graphical substring function with dynamic inputs does not exist from SAP!

Former Member
0 Kudos

hi,

you can not do that dynamically. you need an udf. the solution proposed solves your issue. try using something like this in combination with find first or first occurence of:


public String Mid(String input, String start, String noChars, Container container){
int no = Integer.parseInt(noChars);// this will throw an exception if the noChars is not an integer
		int pStart = Integer.parseInt(start);
		if (pStart > input.length()) 
				return ""; // the starting position is after the end of the string so we can safely return ""
		no += pStart; // now variable no, does not contain only the number of characters but the index where the substring should stop.
		if (pStart + no > input.length()) 
			no = input.length();
		return input.substring(pStart,no);
}

RKothari
Contributor
0 Kudos

Hello Martin,

If position of semicolon in substring is determined at runtime, I guess you need to use UDF.

if(!value.equals("")){

value = value.substring(0,value.indexOf(";"));

}

return value;

-Rahul