2

...so I'm trying to split data using the split() method of string. (And by the way, I'm impressed with how well regular expressions have evolved in Java since I last bent code in a serious fashion.)

Here's what I'm trying, and it doesn't seem to give me results I would predict:

public static void main(String[] args){
    String s = "CSCO_9910290";

    String [] ss = s.split("(.*)_(.*)"); // split on the _ character
    System.out.println("Size of ss is: " + ss.length + "\n"); // this prints 0

    for (String r : ss ){
        System.out.println("result is: " + r + "\n");
    }
    System.out.println("Finished now..."); // declares completion of loop

}

3 Answers 3

5

With the regular expression for split, the aim is not to create a regular expression for the entire string, but for matching the separator(s) only. You don't need anything but:

String [] ss = s.split("_");
Sign up to request clarification or add additional context in comments.

1 Comment

...of course. Blame it on an aging Perl programmer. (not me of course, I'm never to blame.)
1
String [] ss = s.split("_"); // split on the _ character

It is the correct way.

1 Comment

...actually, now that makes sense to me. I was working too hard.
1

For splitting on the _ character simply use s.split("_")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.