In java how to get substring from a string till a character c?

getting a substring from a string after a particular word in java
how to trim a string before a specific character in java
substring java
substring before a character in java
substring before and after in java
string split java
java substring after character
java get string between two strings

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 . (ie a dot)

In java doc api, I can't seem to find a method in String which does that. Am I missing something? How to do it?

look at String.indexOf and String.substring.

Make sure you check for -1 for indexOf.

Get Substring from String in Java, 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 . (ie a dot). In java� The substring (int beginIndex, int endIndex) method of the String class. It returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

The accepted answer is correct but it doesn't tell you how to use it. This is how you use indexOf and substring functions together.

String filename = "abc.def.ghi";     // full file name
int iend = filename.indexOf("."); //this finds the first occurrence of "." 
//in string thus giving you the index of where it is in the string

// Now iend can be -1, if lets say the string had no "." at all in it i.e. no "." is found. 
//So check and account for it.

String subString;
if (iend != -1) 
{
    subString= filename.substring(0 , iend); //this will give abc
}

Java - String substring() Method example, The practical ways of using the useful substring functionality in Java - from based on a character or String we can make use of the indexOf method: Similarly, the substringBefore method gets the substring before the first� String substring(int beginIndex) Returns the substring starting from the specified index i.e beginIndex and extends to the character present at the end of the string. For example – "Chaitanya".substring (2) would return "aitanya". The beginIndex is inclusive, that is why the character present at the index 2 is included in the substring.

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];

Get the substring before the last occurrence of a separator in Java, Learn JSP � Learn JSTL � Learn C � Learn C++ � Learn MongoDB � Learn XML � Learn Java String substring() method is used to get the substring of a given string i.e beginIndex and extends to the character present at the end of the string. before the jump so the length of substring ” jump” is 5 (including the space). String substring(int beginIndex, int endIndex) // Where: // 0 <= beginIndex <= Length of the String // beginIndex <= endIndex <= N It returns the substring starting from the specified index till the end index position.

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]

Java String Between, Before, After, Use the lastIndexOf() method. For that, you need to get the index of the separator using indexOf() String separator ="-"; int sepPos = str.lastIndexOf� Java String substring method is overloaded and has two variants. substring(int beginIndex): This method returns a new string that is a substring of this string.The substring begins with the character at the specified index and extends to the end of this string.

or you may try something like

"abc.def.ghi".substring(0,"abc.def.ghi".indexOf(c)-1);

Searching characters and substring in a String in Java , Implement between, before and after methods to get substrings from strings. Simplify indexOf calls. 1. String substring() : This method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string. Syntax : public String substring(int begIndex) Parameters : begIndex : the begin index, inclusive. Return Value : The specified

Getting Characters by Index from a String or String Buffer, Searching characters and substring in a String in Java int indexOf(char c) // Accepts character as argument, Returns index of // the first Get hold of all the important DSA concepts with the DSA Self Paced Course at a� For example, I have this string: 10.10.10.10/16 and I want to remove the mask from that IP and get: 10.10.10.10 How could this be done?

substring, You can get the character at a particular index within a string, string buffer, The substring methods were added to the StringBuffer class for Java 2 SDK 1.2. Get the position of the first space: int space1 = theString.IndexOf(' '); The the position of the next space after that: int space2 = theString.IndexOf(' ', space1 + 1); Get the part of the string up to the second space: string firstPart = theString.Substring(0, space2); The above code put togehter into a one-liner:

Get first 4 characters of String in Java, Returns a substring of this string that starts at the specified startIndex and of this char sequence starting at the startIndex and ending right before the endIndex . The substring() method returns a substring from the given string. The substring beings with the character at the startIndex and extends to the character at index endIndex - 1. If the endIndex is not passed, the substring beings with the character at the specified index and extends to the end of the string. Working of Java String substring() method

Comments
  • short and sweet. Also remember that indexOf gives the first index of the substring.
  • This is invalid code because you can't put a variable declaration in the body of an If Statement without braces.
  • Your proposal is not correct, because java.lang.String.split takes regex value as attribute, not just string. All "." in your example need to be changed to "\\." Then it will work :)
  • I love regex, but that gets me every time.
  • Cleaner than other solutions.
  • will crash of 'c' is not in the string as indexOf will return -1
  • @Umer Hayat What if the filename is def.hij.klm? How would your code work then? (Your code only works for this one example - you might as well write a function that returns "abc" - it would work just as well)
  • @Bohemian:I know there are boundary cases and checks missing. It was a specific a answer to to the specific question and idea was to give the general idea through an example :).
  • @TofuBeer: Ok, fine there is a confusion earlier "(ie a dot)" wasn't there in actual post. I think and hope my answer gives a general idea to solve this kin of problem.
  • I would go with: int ix = inString.indexOf('.'); if (ix > -1) inString = inString.substring(0, ix); I do not know why Umer suggested subtracting 1. The second argument of substring is up to but excluding anyway.
  • I did not downvote, (and I agree it sucks when people downvote for no reason) but your solution is pretty expensive and not super obvious.
  • @ Knickerless-Noggins I can describe it, if you want specifics I can do that too... but not sure how I could do it in a comment (maybe have to do another question/answer). It is expensive because it creates a whole new string along with all of the replacing the characters. substring simply returns a view into the string. That help?
  • @TofuBeer you are incorrect. Strings are immutable. Substring does not return a view. Every String returned from a String method is a new String.
  • @Knickerless-Noggins He is mistaken... See recent comments. Also, it is efficeiemt enough. The code density is high - it would have to be used intensely to justify adding more code for "performance" sake.
  • It apparently changed in JDK 7, javarevisited.blogspot.ca/2011/10/…