Skip to Content
author's profile photo Former Member
Former Member

How to split a name in two using UDF

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.



Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Posted on Jul 31, 2012 at 06:48 AM

    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).



    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 31, 2012 at 06:58 AM
    StringTokenizer st = new StringTokenizer(s, " "); // s - input stringString str = ""if (st.countTokens() > 1)          {                    st.nextToken();                    str = st.nextToken();          }return str;
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 31, 2012 at 07:33 AM


    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));


    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.


    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 31, 2012 at 12:55 PM

    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



    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.