cancel
Showing results for 
Search instead for 
Did you mean: 

UDF function

Former Member
0 Kudos

Hi Group,

I am new to Java,I have one query related on UDF.

I have a Number 1300015394101HM with lenght 15,if last 2 digits alphabit then I need to take only 13 digits value.Can any body suggest how to write this in JAVA

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

create a UDF with a String a argument

String b = a.substring(13,2);

Boolean isAlp = true;

for (int i=0; i<2; i++)

{

if (!((b.charAt(i)>="A") && (b.charAt(i)<="Z")) || ((b.charAt(i)>="a") && (b.charAt(i)<="z")))

isAlp = false;

}

if (isAlp)

return a.substring(0,13);

else

return "criteria not satisfied";

Thanks,

Rajeev Gupta

henrique_pinto
Active Contributor
0 Kudos

maybe an easier way:

<i>return a.substring(0,13).concat(a.substring(13,2).replaceAll("[^0-9]",""));</i>

That will remove all the non-numeric chars in the last two positions of the string.

Regards,

Henrique.

Former Member
0 Kudos

Hi Henrique,

your suggestion doesnt meet the requirement......see the req again - it is <b>if last 2 digits alphabit then I need to take only 13 digits value</b>....so you dont have to <i>remove all the non-numeric chars in the last two positions of the string</i>

Thanks,

Rajeev Gupta

Message was edited by:

RAJEEV GUPTA

henrique_pinto
Active Contributor
0 Kudos

> Hi Henrique,

>

> your suggestion doesnt meet the requirement......see

> the req again - it is <b>if last 2 digits alphabit

> then I need to take only 13 digits value</b>....so

> you dont have to <i>remove all the non-numeric chars

> in the last two positions of the string</i>

>

> Thanks,

> Rajeev Gupta

>

> Message was edited by:

> RAJEEV GUPTA

It depends.

If the sender application sends only numbers and/or letters at the last 2 positions, than that should satisfy. 😃

Regards,

Henrique.

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

Assume String input is your input number string, then:

// If the 14th character or the 15th character of the input string is an alphabet

if (Character.isLetter(input.charAt(13)) || Character.isLetter(input.charAt(14))) {

//Then return only the first 13 characters

return input.substring(0, 13);

}

//Otherwise, the last two characters are not alphabets, then return the original string

return input;

This will work regardless of the case of the letters and does not assume anything about your input value, and it works faster with the direct return in the if statement.