cancel
Showing results for 
Search instead for 
Did you mean: 

UDF code needed for splitting data string

Former Member
0 Kudos

Hi All,

I am trying to create a UDF for following functionality:

( Need to copy only 5 characters of an incoming data string(A) starting from a particular character(B) )

Input 1 ( Text/integer String) = A

Input 2 ( Split Start Place) = B

Output ( Split value string) = C

Example:

Incoming string(A) = '00112233445566778899'

Split start value(B) = '3'

Output (C) = '11223'.

Appreciate if you could let me know the code for it.

Thanks

Shirin

View Entire Topic
prateek
Active Contributor
0 Kudos

Use standard function "substring"

Regards,

Prateek

Former Member
0 Kudos

HI Prateek,

I am aware of this function, but can not use it. My previous post on sdn will explain that( if interested). UDF is my last resort .

To make the long story short, I need this UDF to make my scenario work

Thanks

Shirin

former_member181985
Active Contributor
0 Kudos

Hi,

Try this code,

String source = "00112233445566778899";

String checkChar = "3";

String target = "";

if(source.indexOf(checkChar) != -1)

target=source.substring(source.indexOf(checkChar)-4, source.indexOf(checkChar)+1);

System.out.println(target);

Change the code dynamically according to your requirement. I hardcoded the values.

Thanks,

Gujjeti

Former Member
0 Kudos

Hi Praveen and Soumya,

Thanks for the code. I am zero in Java programming. Both these codes are returning error messages and I have no idea how to corect it.

Apologies, not sounding rude, but am just helpless. If possible could you pass me the code which I could just cut and paste in the function box.

Thanks again for the replies.

Regards

Shirin

former_member181985
Active Contributor
0 Kudos

Hi shirin,

Create a new UDF without changing default options and put the following code in the UDF editor and map the input & output fields accordingly to the UDF in the mapping editor.

//--


CODE--


String checkChar = "3"; //Fixed char

if(a.indexOf(checkChar) != -1 && a.indexOf(checkChar) >=4 )

return ( a.substring(a.indexOf(checkChar)-4, a.indexOf(checkChar)+1) );

else

return "Input String Doesnt have the expected char occurence (or) the occurence is before the 5 th position";

//--


CODE--


If the fixed char is from source structure as input field, then change the first statement as,

String checkChar = b;

Means your UDF has two inputs a and b.

If it is not the case, then simply use the above code.

Thanks,

Gujjeti

Former Member
0 Kudos

Hi,

Create the UDF and select function as simple.

And give the variable as A (the entire string) and B (Its a character where you want to take the substring)

String s=a.substring('b',5);

// here b is the starting character

return s;