cancel
Showing results for 
Search instead for 
Did you mean: 

substring with numbers

Former Member
0 Kudos

Hi XI masters,

Does anyone know how to cut an input so that only the first numbers would be left?

Ex. 100_hello -> 100

98_hello -> 98

1000_computer -> 1000

I tried using substring (0,3) then I realized that the numbers could have different digits. They are not always 3.

Accepted Solutions (1)

Accepted Solutions (1)

markangelo_dihiansan
Active Contributor
0 Kudos

Hi,

This is only possible via UDF.

UDF type is ContextType,

input argument is input

for(int a=0;a<input.length;a++){

result.addValue(input[a].substring(0,input[a].indexOf("_")));

}

hope this helps,

Answers (1)

Answers (1)

sunilchandra007
Active Contributor
0 Kudos

Hi,

Try this one !!!

public String beforeUnderScore(String a, Container container)
{
String arr[] = a.split("_");
return arr[0];
}

Regards,

Sunil Chandra

Former Member
0 Kudos

i just found out that not all the values have underscores. they can be any character. how will I proceed? thanks!

former_member187339
Active Contributor
0 Kudos

Helllo..

Try this UDF, it should help


int j=0;

for(int a=0;a<input.length;a++){
  for(j=0;i< input[a].length();j++) {
    //If we find a non-digit character we return false.
    if (!Character.isDigit(input[a].charAt(j)))
               break;
  }
  result.addValue(input[a].substring(0,j));
  j=0;
}

Regards

Suraj