on 05-30-2008 2:48 PM
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
Use standard function "substring"
Regards,
Prateek
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
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.
//--
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";
//--
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Srini,
Send me those error messages..
Regards,
Soumya
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
int l = A.index(b);
String s=a.substring(l,5);
return s;
In the s variable you got the required output.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
91 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.