on 07-31-2012 7:37 AM
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
StringTokenizer st = new StringTokenizer(s, " "); // s - input string
String str = ""
if (st.countTokens() > 1)
{
st.nextToken();
str = st.nextToken();
}
return str;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
88 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
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.