0

I have a string which contains an underscore as shown below:

123445_Lisick

I want to remove all the characters from the String after the underscore. I have tried the code below, it's working, but is there any other way to do this, as I need to put this logic inside a for loop to extract elements from an ArrayList.

public class Test {
    public static void main(String args[]) throws Exception {
        String str = "123445_Lisick";
        int a = str.indexOf("_");
        String modfiedstr = str.substring(0, a);
        System.out.println(modfiedstr);
    }
}
Laf
  • 7,965
  • 4
  • 37
  • 52
Pawan
  • 31,545
  • 102
  • 256
  • 434
  • 3
    I'm sure there are other ways of doing this, but what's wrong with the way you're doing it? It looks like an efficient way of accomplishing your task. – rgettman Oct 09 '13 at 16:06
  • You could use regular expressions but I don't see an advantage and it would probably be less efficient. See this post: http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java – Alex Theedom Oct 09 '13 at 16:06
  • An alternative method is using a regex with `String#replaceAll(String,String)` or `split` – nachokk Oct 09 '13 at 16:07
  • Do it the way you do it now. – Ortwin Angermeier Oct 09 '13 at 16:10

3 Answers3

5

Another way is to use the split method.

String str = "123445_Lisick";
String[] parts = string.split("_");
String modfiedstr = parts[0];

I don't think that really buys you anything though. There's really nothing wrong with the method you're using.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 1
    This also nicely works when there's no underscore at all. (Which may, or may not, be wanted.) – Arjan Oct 09 '13 at 16:15
  • [is it always safe to use the first element of the array returned from split?](http://stackoverflow.com/questions/9743159/is-it-always-safe-to-use-the-first-element-of-the-array-returned-by-split) – Obicere Oct 09 '13 at 16:17
0

Your method is fine. Though not explicitly stated in the API documentation, I feel it's safe to assume that indexOf(char) will run in O(n) time. Since your string is unordered and you don't know the location of the underscore apriori, you cannot avoid this linear search time. Once you have completed the search, extraction of the substring will be needed for future processing. It's generally safe to assume the for simple operations like this in a language which is reasonably well refined the library functions will have been optimized.

Note however, that you are making an implicit assumption that

  • an underscore will exist within the String
  • if there are more than one underscore in the string, all but the first should be included in the output

If either of these assumptions will not always hold, you will need to make adjustments to handle those situations. In either case, you should at least defensively check for a -1 returned from indexAt(char) indicating that '_' is not in the string. Assuming in this situation the entire String is desired, you could use something like this:

public static String stringAfter(String source, char delim) {
     if(source == null) return null;
     int index = source.indexOf(delim);
     return (index >= 0)?source.substring(index):source;
}
Andrew Ring
  • 3,185
  • 1
  • 23
  • 28
0

You could also use something like that:

public class Main {
  public static void main(String[] args) {
    String str = "123445_Lisick";
    Pattern pattern = Pattern.compile("^([^_]*).*");
    Matcher matcher = pattern.matcher(str);
    String modfiedstr = null;
    if (matcher.find()) {
      modfiedstr = matcher.group(1);
    }
    System.out.println(modfiedstr);
  }
}

The regex groups a pattern from the start of the input string until a character that is not _ is found.

However as @Bill the lizard wrote, i don't think that there is anything wrong with the method you do it now. I would do it the same way you did it.

Ortwin Angermeier
  • 5,957
  • 2
  • 34
  • 34