1

I have used Split method on a String that contains + - and | and i dont wanna get that empty value, here's my code

public class Test {

   public static void main(String args[]) {

     String ab="+computer-science|lambda+sigma";
     String[] lesAndz = ab.split("[\\W]");
     for(int i=0;i<lesAndz.length;i++){
                    System.out.println(i+" : ["+lesAndz[i]+"]");

      }

}

}

I want it to output

0 : [computer]
1 : [science]
2 : [lambda]
3 : [sigma]

but im getting

0 : []
1 : [computer]
2 : [science]
3 : [lambda]
4 : [sigma]

I tried with

String[] lesAndz = ab.split("[\\W]", -1);

and it didnt work

0

6 Answers 6

0

I would replace the separating characters with spaces. Then you can use trim() to remove any extra characters at either end before you split. I'm assuming you don't want any empty strings in the results, so I've changed the expression slightly.

ab.replaceAll("\\W+", " ").trim().split(" ")
Sign up to request clarification or add additional context in comments.

Comments

0

According the Java Doc, https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)

the trailing empty string will not be included, but the leading empty string will be. You just have do another check to see if the leading string is an empty and remove it from your list.

Comments

0

You can check out this one. This guy had pretty much the same problem. You can get some clue.

1 Comment

Please read How do I write a good answer? before attempting to answer more questions.
0

Before splitting ,check ab.indexOf(“”) ==0 then only substring code above else use the original code

Comments

0

You can't do it (discard leading/all empty values...at least not with java.langString#split()), since:

When there is a positive-width match at the beginning of this string then an empty leading substring is included at the beginning of the resulting array. A zero-width match at the beginning however ...

+ has a positive-width match...

And as I understand, you can prevent/omit only trailing empty strings ... then with split(rex, 0) or split(rex).

...and (e.g.) with "+some+thing++foo++++++++".split("\\W"), you'll still get {"", "some", "thing", "", "foo"}

(recommendation) --> lambda expression or some own/org.apache...StringUtils wrapper.

...or a double invocation with replace(), as per accepted answer, nice! lol

Comments

0

You could cut off the the first character using substring:

String ab = "+computer-science|lambda+sigma";
String[] lesAndz;
if(Character.toString(ab.charAt(0)).matches("[\\W]")) {
    lesAndz = ab.substring(1,ab.length()).split("[\\W]");
} else {
    lesAndz = ab.split("[\\W]");
}
for(int i=0;i<lesAndz.length;i++){
    System.out.println(i+" : ["+lesAndz[i]+"]");
}

Output:

0 : [computer]
1 : [science]
2 : [lambda]
3 : [sigma]

Works for both, strings starting with an alpha-numeric and strings starting with a non-alpha-numeric character, e.g.:

  • +computer-science|lambda+sigma
  • computer-science|lambda+sigma

2 Comments

The problem is that in my main code sometimes the first caracter isnt always empty, i tried to check that if and if(firstchar=="") but its nor working
I edited the solution to check the first character before splitting the string

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.