I have a string (which is basically a file name following a naming convention) abc.def.ghi
I would like to extract the substring before the first
If your project already uses commons-lang, StringUtils provide a nice method for this purpose:
String filename = "abc.def.ghi";
String start = StringUtils.substringBefore(filename, "."); // returns "abc"
see javadoc [2.6] [3.1]
You can just split the string..
public String[] split(String regex)
Note that java.lang.String.split uses delimiter's regular expression value. Basically like this...
String filename = "abc.def.ghi"; // full file name
String[] parts = filename.split("\\."); // String array, each element is text between dots
String beforeFirstDot = parts[0]; // Text before the first dot
Of course, this is split into multiple lines for clairity. It could be written as
String beforeFirstDot = filename.split("\\.")[0];
or you may try something like
"abc.def.ghi".substring(0,"abc.def.ghi".indexOf(c)-1);