What is the "substring trap" in Java?
String.substring(int beginIndex, int endIndex) is a way of extracting a new string that is a substring of a String. The offsets are 0-based, i.e. the first character of the String is character 0. The name endIndex suggests that is the index where the Substring ends. Do not be confused and mislead by endIndex, the largest endIndex is one character past the end of the String.
According to String APIs document, "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".
For example,
class Program {
public static void main(String[] args) {
String s="substring trap";
System.out.println(s.substring(3,9));
}
}
The output is: string.
Most Recent java Faqs
- How to uncompress a file in the gzip format?
- How to make a gzip file in Java?
- How to use Java String.split method to split a string by dot?
- How to validate URL in Java?
- How to schedule a job in Java?
- How to return the content in the correct encoding from a servlet?
- What is the difference between JDK and JRE?
Most Viewed java Faqs
- How to read input from console (keyboard) in Java?
- How to use HttpURLConnection POST data to web server?
- How to add BASIC Authentication into HttpURLConnection?
- How to Retrieve Multiple Result Sets from a Stored Procedure in JDBC?
- What are class variables in Java?
- What are local variables in Java?
- How to Use Updatable ResultSet in JDBC?