Skip to Content

UDF Required to get the String value before "-"

Hello Experts,
I have been stuggling from past 5 days to get this done via UDF, have found a number of the UDFs across the web although none of them worked to fulfill my requirement completely here.

This was the closest one I found :

UDF Required to get the string before pipe

The solution provided in the above case is getting me only the first vlaue, However, in my case I need all the values eg. 1;3;4;7

Input value : 1-Dummy1; 3-Dummy2Dummy; 4-DummyISO4000; 7-MAN IN THE BOX

Expected Output value: 1;3;4;7

OR COULD BE THIS

Input value: 1-DUMMY2; 2-DUMMY3; 5-DUMMY TIP SYMBOL

Expected Output value: 1;2;5

OR COULD BE THIS

Input value: NA

Expected Output value: NA

quick help would be appreciated.

Add a comment
10|10000 characters needed characters exceeded

Related questions

2 Answers

  • Best Answer
    Posted on Sep 04 at 11:09 AM

    Hello Ryan Crosby,

    Thank you for your assistance and I after making a few fixes, the piece of code did work exactly how I wanted.

    Here is the Code, avaialable for eveyone with perfect result.

    String output = "", source;

    String[] splits = input.split(";");

    if(input.indexOf("-")>-1){

    for(int i = 0; i < splits.length -1; i++) {

    source = splits[i].trim();

    output += source.substring(0, source.indexOf('-')) + ";";

    }

    source = splits[splits.length -1].trim();

    output += source.substring(0, source.indexOf('-')) + ";";

    return output.substring(0, output.length() - 1) ;

    }

    else{ return input;

    }

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 25 at 12:52 PM

    Hi,

    This is a very simple function so you shouldn't need to find it... you should be able to write it. Since it is quite simple here is code that will do what you have described as you have described it. The reference to input is the name of the value passed into the UDF which is unknown from my perspective.

          String output = "", source;
          String[] splits = input.split(";");
          for(int i = 0; i < splits.length - 1; i++) {
            source = splits[i].trim();
            output += source.substring(0, source.indexOf('-')) + ";";
          }
          source = splits[splits.length -1].trim();
          output += source.substring(0, source.indexOf('-')) + ";";
          return output;

    Regards,

    Ryan Crosby

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.