cancel
Showing results for 
Search instead for 
Did you mean: 

How to split a name in two using UDF

Former Member
0 Kudos

Hi Experts,

I have a situation here. In need to split the name into two sub names and take the last name as output.

For example, if i take the name 'Sachin Tendulkar' as input i should get the output as Tendulkar. For this i have written the code as shown below.

String[] tokens = str.split("\\s"); /str is input/

String fin=tokens[1];

return fin;

I am getting the output as Tendulkar. But when the input is coming without a delimiter like 'sachinTendulkar" i am getting error.Then the ouyput should be none. and also when ever the name comes has sachin Ramesh Tendulkar the output should come as 'Ramesh Tendulkar' and not  'ramesh'.

Or is there any way directly to do mapping. I hope you understood my problem.

Thanks,

Srinivas.

Accepted Solutions (0)

Answers (4)

Answers (4)

anupam_ghosh2
Active Contributor
0 Kudos

Hi Srinivas,

                  you can continue with split method with minor changes in your code.

You can try this

public String TargetFilename(String s,Container container){  

  String b="";

  try

  {

  

   String a[]=s.split(" ");

  

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

   {

    b=b+" "+a[i];

   }

  }

  catch(Exception e)

  {

   e.printStackTrace();

  }

  return b;

}

here are the corrosponding input and output

 

input=Sachin ramesh Tendulkar, output= ramesh Tendulkar

input=SachinrameshTendulkar, output=

input=Sachinramesh Tendulkar, output= Tendulkar

Regards

Anupam

iaki_vila
Active Contributor
0 Kudos

Hi,

A few cents,

This solution uses a UDF to make a new substring with the last index of the blank char:

The UDF:

public String dynamicSubstring(String stringToSplit, String index, Container container) throws StreamTransformationException{

int begin =0;

int end = 0;

try {

     begin=Integer.parseInt(index) + 1;

     end =  stringToSplit.length();

}

catch(NumberFormatException e) { }

if (begin > 0)

  return (stringToSplit.substring(begin,end));

else

  return  "";

}

This solution is a bit more complicated that the another one but you can use the substring function with the index as parameter in the future.

Regards.

former_member181985
Active Contributor
0 Kudos

StringTokenizer st = new StringTokenizer(s, " "); // s - input string

String str = ""

if (st.countTokens() > 1)

          {

                    st.nextToken();

                    str = st.nextToken();

          }

return str;

former_member184681
Active Contributor
0 Kudos

Hi,

Looks nice with this StringTokenizer class, but I have two doubts:

1. Why tokenize the whole string and then concatenate it back, when you can get the final output with a single command, like in my code?

2. Your code always returns the last token of the input, so for "sachin Ramesh Tendulkar" it will return "Tendulkar", and the requirement is to return "Ramesh Tendulkar".

Regards,

Greg

former_member181985
Active Contributor
0 Kudos

Greg, you are right. I agree.

former_member184681
Active Contributor
0 Kudos

Hi Srinivas,

Use the following code to achieve your required output:

int index = name.indexOf(' ');

if (index > 0) return name.substring(index+1, name.length());

else return "";

The UDF takes one input parameter name and Execution Type should be set to Single Values.

Also, keep in mind that this UDF is sort of "naive" - it always returns the input part starting from the first blank. Some situations might occur when this is not the last name (when a person has two first names, or so).

Regards,

Greg