Skip to Content
avatar image
Former Member

substring with numbers

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.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Nov 12, 2009 at 07:40 AM

    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,

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 12, 2009 at 07:45 AM

    Hi,

    Try this one !!!

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

    Regards,

    Sunil Chandra

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      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