Java substring: 'string index out of range'

后端 未结 13 749
眼角桃花
眼角桃花 2020-12-03 10:24

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

相关标签:
13条回答
  • 2020-12-03 10:47

    When this is appropriate, I use matches instead of substring.

    With substring:

    if( myString.substring(1,17).equals("Someting I expect") ) {
        // Do stuff
    }
    // Does NOT work if myString is too short
    

    With matches (must use Regex notation):

    if( myString.matches("Someting I expect.*") ) {
        // Do stuff
    }
    // This works with all strings
    
    0 讨论(0)
  • 2020-12-03 10:48

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

    0 讨论(0)
  • 2020-12-03 10:50

    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...

    0 讨论(0)
  • 2020-12-03 10:55

    I'm assuming your column is 38 characters in length, so you want to truncate itemdescription to fit within the database. A utility function like the following should do what you want:

    /**
     * Truncates s to fit within len. If s is null, null is returned.
     **/
    public String truncate(String s, int len) { 
      if (s == null) return null;
      return s.substring(0, Math.min(len, s.length()));
    }
    

    then you just call it like so:

    String value = "_";
    if (itemdescription != null && itemdescription.length() > 0) {
      value = truncate(itemdescription, 38);
    }
    
    pstmt2.setString(3, value);
    
    0 讨论(0)
  • 2020-12-03 10:56

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

    0 讨论(0)
  • 2020-12-03 10:57

    You get this if itemdescription is shorter than 38 characters

    You can look which exceptions are thrown and when in the JAVA API in you case for String#substring(int,int): https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#substring-int-int-

    substring
    public String substring(int beginIndex, int endIndex)
       . . .
    
    Throws:
     IndexOutOfBoundsException
     if the beginIndex is negative,
     or endIndex is larger than the length of this String object, 
     or beginIndex is larger than endIndex.
    
    
    
    (same applies to previous java versions as well)
    
    0 讨论(0)
提交回复
热议问题