Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string with dot as delimiter

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.

like image 684
Dean Avatar asked Aug 02 '10 12:08

Dean


People also ask

Is Dot a delimiter?

Unlike comma, colon, or whitespace, a dot is not a common delimiter to join String, and that's why beginner often struggles to split a String by dot.

How do you split a string in python with dot?

String split example s = "Python string example. We split it using the dot character." parts = s. split(".")

How do you split a string by a space and a comma?

To split a string by space or comma, pass the following regular expression to the split() method - /[, ]+/ . The method will split the string on each occurrence of a space or comma and return an array containing the substrings.

How do you check if a string contains a dot in Java?

The easiest way is to check with a . contains() statement.


1 Answers

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]; 
like image 166
Marimuthu Madasamy Avatar answered Oct 13 '22 19:10

Marimuthu Madasamy