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

Accepted Solutions (1)

Accepted Solutions (1)

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;

Answers (7)

Answers (7)

Former Member
0 Kudos

Hi,

Thanks for all the replies. I keep on getting error messages for the UDF source code. But have manged to solve the issue using the existing standard graphical mapping functions.

If anyone needs to know, do send me your comm id and I can send the screen shot of my mapping as I can not paste it here.

Thanks once again everyone

Shirin

Former Member
0 Kudos

Hi Shiri,

Try this UDF code

String vals[] = A[0].split("3");

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

{

result.addValue(vals[j]);

}

Thanks

Vikranth

Former Member
0 Kudos

Hi,

try this code:


// inputString: input string parameter
// start_char: input string of the starting char

int NUM_VALID_CHAR = 5; 
int index_start = (inputString.indexOf("start_char") + 1) - NUM_VALID_CHAR;

/*
* I put a check if you have an input string where the starting char 
* have an index less then 4. You can delete it if you don't need it.
*/
if(index_start >= 0){
	return inputString.substring(index_start, index_start + NUM_VALID_CHAR);
}else{
	
                return inputString.substring(0 , NUM_VALID_CHAR+1);
}

Regards

Emiliano

Former Member
0 Kudos

Hi Srini,

Send me those error messages..

Regards,

Soumya

Former Member
0 Kudos

Are you looking at removing leading zeroes and then extracting the 5 characters. Then you write a simple udf, to remove leading zeroes like this

return a.replaceAll("^0+","");

a is the input parameter.

and then you can use the substrign function.

Former Member
0 Kudos

Hi,

public String mapxxx(String a,Container container)

{

int b=3;

if(a.length()>0)

{

String b=a.substring(3,5);

return b;

}

else

return a;

}

check this..

correct me if wrong.

Regards,

Soumya

Former Member
0 Kudos

Hi,

int l = A.index(b);

String s=a.substring(l,5);

return s;

In the s variable you got the required output.