Asked  7 Months ago    Answers:  5   Viewed   72 times

I am wondering if I am going about splitting a string on a . the right way? My code is:

String[] fn = filename.split(".");
return fn[0];

I only need the first part of the string, that's why I return the first item. I ask because I noticed in the API that . means any character, so now I'm stuck.

 Answers

98

split() accepts a regular expression, so you need to escape . to not consider it as a regex meta character. Here's an example :

String[] fn = filename.split("\."); 
return fn[0];
Tuesday, June 1, 2021
 
Sujith
answered 7 Months ago
62

Luckily, Python has this built-in :)

import re
re.split('; |, ',str)

Update:
Following your comment:

>>> a='Beautiful, is; better*thannugly'
>>> import re
>>> re.split('; |, |*|n',a)
['Beautiful', 'is', 'better', 'than', 'ugly']
Tuesday, June 1, 2021
 
Claudio
answered 7 Months ago
33

You could do it like this:

String s = "1234567890";
System.out.println(java.util.Arrays.toString(s.split("(?<=\G...)")));

which produces:

[123, 456, 789, 0]

The regex (?<=G...) matches an empty string that has the last match (G) followed by three characters (...) before it ((?<= ))

Friday, June 4, 2021
 
kinske
answered 7 Months ago
82

You have one small mistake in your regex. Try this:

String[] Res = Text.split("[\p{Punct}\s]+");

[\p{Punct}\s]+ move the + form inside the character class to the outside. Other wise you are splitting also on a + and do not combine split characters in a row.

So I get for this code

String Text = "But I know. For example, the word "can't" should";

String[] Res = Text.split("[\p{Punct}\s]+");
System.out.println(Res.length);
for (String s:Res){
    System.out.println(s);
}

this result

10
But
I
know
For
example
the
word
can
t
should

Which should meet your requirement.

As an alternative you can use

String[] Res = Text.split("\P{L}+");

\P{L} means is not a unicode code point that has the property "Letter"

Thursday, June 24, 2021
 
Chvanikoff
answered 6 Months ago
20

This will work even when you have many number of underscore characters in the string you wanted.

str.substring(str.indexOf("_", str.indexOf("_") + 1) + 1, str.lastIndexOf("_"));
Tuesday, August 17, 2021
 
Farkin
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share