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

Fragan
  • 792
  • 10
  • 29

6 Answers6

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
Rene Knop
  • 1,788
  • 3
  • 15
  • 27
  • 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 – Fragan May 26 '18 at 21:19
  • I edited the solution to check the first character before splitting the string – Rene Knop May 26 '18 at 21:37
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.

Heapify
  • 2,581
  • 17
  • 17
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(" ")
ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
0

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

Ratnadeep
  • 1,125
  • 1
  • 15
  • 30
  • Please read [How do I write a good answer?](http://stackoverflow.com/help/how-to-answer) before attempting to answer more questions. –  May 26 '18 at 21:50
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

xerx593
  • 12,237
  • 5
  • 33
  • 64
0

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

Tarun Sawlani
  • 34
  • 1
  • 4