Java String.split : Trouble using \\W as non word delimiter
12,509
The ,
and the space are been treated as separate entities, try using \\W+
instead
String str = "id-INT, name-STRING,";
String[] parts = str.split("\\W+");
System.out.println(parts.length);
System.out.println(Arrays.toString(parts));
Which outputs
4
[id, INT, name, STRING]
Author by
user3527975
Updated on July 14, 2022Comments
-
user3527975 almost 2 years
Going by the suggestion provided here I tried using \\W as a delimiter for non word character in string.split function of java.
String str = "id-INT, name-STRING,";
This looks like a really simple string. I wanted to extract just the words from this string. The length of the array that I get is 5 whereas it should be 4. There is an empty string at position right after INT. I don't understand why the space in there is not being considered as non word
-
user3527975 about 9 yearsI tried googling the significance of + sign here. Didn't get any. Could you please explain what did the + sign do? BTW : Thank you very much
-
MadProgrammer about 9 yearsFrom my (puny) understanding, it's grouping all the entities together, so rather than using the individual elements, it's group all the matches that occur simultaneously (
", "
) together into a single match