Skip to Content
avatar image
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.

Thanks,

Srinivas.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

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

    Regards,

    Greg

    Add comment
    10|10000 characters needed characters exceeded

  • 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 comment
    10|10000 characters needed characters exceeded

  • Jul 31, 2012 at 07:33 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • 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

    Regards

    Anupam

    Add comment
    10|10000 characters needed characters exceeded