on 12-07-2010 1:48 AM
Hi Masters,
I want find the first letter from input, Can any one help me on this..UDF or any solution.
Ex: E2HB means - Alpha Letter is the first
Ex: 1234 means - Number is the first
Thanks,
Siva
Hi,
Try using below logic to determine if the first character is alphabet or number.
String firstChar = "";
int Num;
firstChar = input.trim().substring(0,1);
try{
Num = Integer.parseInt(firstChar);
return "Number";
}
catch(Exception e)
{
return "Alphabet";
}
Regards,
Rahul
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Rahul,
I am poor in JAVA code, do i need declare any thing, i am getting error...
symbol : variable input location: class com.sap.xi.tf._MM_Transaction_ firstChar = input.trim().substring(0,1); ^ /usr/sap/X6D/DVEBMGS23/j2ee/cluster/server0/./temp/classpath_resolver/Mape0858290022011e0826700212887e310/source/com/sap/xi/tf/_MM_Transaction_.java:1871: cannot return a value from method whose result type is void return a; ^ /usr/sap/X6D/DVEBMGS23/j2ee/cluster/server0/./temp/classpath_resolver/Mape0858290022011e0826700212887e310/source/com/sap/xi/tf/_MM_Transaction_.java:1875: cannot return a value from method whose result type is void return b; ^ 3 errors
Thanks
Edited by: Mr Siva on Dec 7, 2010 8:00 PM
Hello,
You can simply use the substring function for this, e.g field --> substring(0..1) --> target. Just make sure that you incorporate additional logic for checking null values.. e.g field --> mapWithDefault --> substring(0..1) --> target.
Hope this helps,
Mark
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Siva,
I just want to clarify this doubt, you want the first character of the string you pass to the UDF i.e if input="E2HB" output="E" and if input="1234" output="1". If my assumption is correct you can try the UDF "firstChar" I have shown below this gives exactly the output you want
public class firstLetter {
public static String firstChar(String s)
{
if(s==null)
{
return null;
}
if(s.equals(""))
{
return s;
}
String t="";
t+=s.charAt(0);
return t;
}
public static void main(String[] args) {
String s1="E2HB",s2="1234";
s1=firstChar(s1);
System.out.println(s1);
s2=firstChar(s2);
System.out.println(s2);
}
}
Regards
Anupam
User | Count |
---|---|
88 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.