cancel
Showing results for 
Search instead for 
Did you mean: 

UDF for Find the First letter from Input

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (2)

Answers (2)

RKothari
Contributor
0 Kudos

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

Former Member
0 Kudos

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

RKothari
Contributor
0 Kudos

Hi,

I hope you are using Single values as an option in the UDF.

No context or queue option should be mentioned.

Also, declare the variable name as "input" .

-Rahul

markangelo_dihiansan
Active Contributor
0 Kudos

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

Former Member
0 Kudos

Any other ideas pls....

Former Member
0 Kudos

Hi Siva,

Even if you do a UDF you will still be using the same function in Java (i.e. Using substring) so why not use the standard function as it is there for a reason.

regards

mimran

anupam_ghosh2
Active Contributor
0 Kudos

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

xolani1208
Explorer
0 Kudos

Hello Mark

Thank you for your your comment. Right in on the spot Your answer really helped me a lot.

Regards
Patrick