Java substring: 'string index out of range'

java substring indexof
java substring from end
java substring regex
java substring 0 0
java substring geeks
java substring practice
find substring in string java
substring in java w3schools

I'm guessing I'm getting this error because the string is trying to substring a null value. But wouldn't the ".length() > 0" part eliminate that issue?

Here is the Java snippet:

if (itemdescription.length() > 0) {
    pstmt2.setString(3, itemdescription.substring(0,38));
} 
else { 
    pstmt2.setString(3, "_");
} 

I got this error:

 java.lang.StringIndexOutOfBoundsException: String index out of range: 38
    at java.lang.String.substring(Unknown Source)
    at MASInsert2.itemimport(MASInsert2.java:192)
    at MASInsert2.processRequest(MASInsert2.java:125)
    at MASInsert2.doGet(MASInsert2.java:219)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:835)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:640)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1286)
    at java.lang.Thread.run(Unknown Source)

I"m guessing i'm getting this error because the string is trying to substring a Null value. But wouldn't the ".length() > 0" part eliminate that issue?

No, calling itemdescription.length() when itemdescription is null would not generate a StringIndexOutOfBoundsException, but rather a NullPointerException since you would essentially be trying to call a method on null.

As others have indicated, StringIndexOutOfBoundsException indicates that itemdescription is not at least 38 characters long. You probably want to handle both conditions (I assuming you want to truncate):

final String value;
if (itemdescription == null || itemdescription.length() <= 0) {
    value = "_";
} else if (itemdescription.length() <= 38) {
    value = itemdescription;
} else { 
    value = itemdescription.substring(0, 38);
}
pstmt2.setString(3, value);

Might be a good place for a utility function if you do that a lot...

Substring in Java, 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 :  Description 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 or up to endIndex – 1, if the second argument is given.

It is a pity that substring is not implemented in a way that handles short strings – like in other languages e.g. Python.

Ok, we cannot change that and have to consider this edge case every time we use substr, instead of if-else clauses I would go for this shorter variant:

myText.substring(0, Math.min(6, myText.length()))

Java String substring() Method example., 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. Java String substring() method is used to get the substring of a given string based on the passed indexes. There are two variants of this method. In this guide, we will see how to use this method with the help of examples. String substring() method variants

You really need to check if the string's length is greater to or equal to 38.

String (Java Platform SE 7 ), Java String substring() example. Learn to get substring of a string from given start index to end index. A valid index is between 0 and length of string. There are two variants of substring() method.This article depicts about all of them, as follows : 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.

I would recommend apache commons lang. A one-liner takes care of the problem.

pstmt2.setString(3, StringUtils.defaultIfEmpty(
    StringUtils.subString(itemdescription,0, 38), "_")); 

Java String substring() method example, A part of string is called substring. In other words, substring is a subset of another string. In case of substring startIndex is inclusive and endIndex is exclusive. Note:​  The java string substring() method returns a part of the string. We pass begin index and end index number position in the java substring method where start index is inclusive and end index is exclusive. In other words, start index starts from 0 whereas end index starts from 1. There are two types of substring methods in java string.

substring(0,38) means the String has to be 38 characters or longer. If not, the "String index is out of range".

Substring in Java, Java String substring() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string substring in​  Java String substring(int beginIndex, int endIndex) example It returns a string that is a substring of the string. The substring begins with the character at the specified 'beginIndex' (included) to the 'endIndex' (excluded) position. Java program to get substring of a string from given start index to end index.

Java String substring() method, 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  In this quick tutorial, we'll focus on the substring functionality of Strings in Java. We'll mostly use the methods from the String class and few from Apache Commons' StringUtils class. In all of the following examples, we're going to using this simple String: String text = "Julia Evans was born on 25-09-1984.

Java String substring() Method Examples, Java Substring v2. Video. Simple substring(start) v1 -- previous video; str.​substring Duration: 6:37 Posted: Nov 16, 2012 The substring () method extracts the characters from a string, between two specified indices, and returns the new sub string. This method extracts the characters in a string between "start" and "end", not including "end" itself. If "start" is greater than "end", this method will swap the two arguments, meaning str.substring (1, 4) == str

Java Substring v2, how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. The substring() method extracts the characters from a string, between two​  The Java language provides special support for the string concatenation operator (+), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder (or StringBuffer) class and its append method.

Comments
  • Yes, this is so ridiculous.
  • I'd be really interested in knowing what itemdescription.substring(0, itemdescription.length()) would return :)
  • var itemdescription = new String("Hello, World!"); alert( itemdescription.substring(0, itemdescription.length) ); returns "Hello, World!".
  • He probably wanted to do something with it.
  • hmm, yes, should have had an upper bound there. that's midnight posting for you...
  • Comparing strings with == will not work in most of case. Pattern matching is quite different from string comparison, and in this case it is a bad idea, notably (but not only) for performance reason.
  • You're right Alexandre; changed for equals. I find Regex very handy and easy to use, but it's right also that it is less performant.