解決java.lang.StringIndexOutOfBoundsException: String index out of range: -1錯(cuò)誤問(wèn)題
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
字符串截取下標(biāo)越界
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1967)
出錯(cuò)代碼
result.put("value", valueBuilder.toString().substring(0,valueBuilder.toString().length()-1));
修改后代碼
if (StringUtils.isNotBlank(valueBuilder.toString()) && valueBuilder.toString().length() >0){ result.put("value", valueBuilder.toString().substring(0,valueBuilder.toString().length()-1)); }
心得:
- StringIndexOutOfBoundsException 異常源碼如下:
/** * Thrown by {@code String} methods to indicate that an index * is either negative or greater than the size of the string. For * some methods such as the charAt method, this exception also is * thrown when the index is equal to the size of the string. */ public class StringIndexOutOfBoundsException extends IndexOutOfBoundsException { private static final long serialVersionUID = -6762910422159637258L; /** * Constructs a {@code StringIndexOutOfBoundsException} with no * detail message. * * @since JDK1.0. */ public StringIndexOutOfBoundsException() { super(); } /** * Constructs a {@code StringIndexOutOfBoundsException} with * the specified detail message. * * @param s the detail message. */ public StringIndexOutOfBoundsException(String s) { super(s); } /** * Constructs a new {@code StringIndexOutOfBoundsException} * class with an argument indicating the illegal index. * * @param index the illegal index. */ public StringIndexOutOfBoundsException(int index) { super("String index out of range: " + index); } }
總共有以下幾個(gè)方法會(huì)拋出該異常:
String.substring() String.charAt() String.codePointAt() String.codePointBefore() String.subSequence() String.getChars() String.getBytes()
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java線(xiàn)程間共享實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Java線(xiàn)程間共享實(shí)現(xiàn)方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10springboot中關(guān)于自動(dòng)建表,無(wú)法更新字段的問(wèn)題
這篇文章主要介紹了springboot中關(guān)于自動(dòng)建表,無(wú)法更新字段的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02詳解Java8如何使用Lambda表達(dá)式進(jìn)行比較
Lambda表達(dá)式,也可稱(chēng)為閉包,是java8的新特性,作用是取代大部分內(nèi)部類(lèi),優(yōu)化java代碼結(jié)構(gòu),讓代碼變得更加簡(jiǎn)潔緊湊。本文將利用Lambda表達(dá)式進(jìn)行排序比較,需要的可以參考一下2022-01-01使用Spring boot標(biāo)記一個(gè)方法過(guò)時(shí)
這篇文章主要介紹了使用Spring boot標(biāo)記一個(gè)方法過(guò)時(shí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12基于Spring監(jiān)聽(tīng)Binlog日志的方法詳解
MySQL 的二進(jìn)制日志(binlog)有三種不同的格式,通常被稱(chēng)為 binlog 模式,這三種模式分別是 Statement 模式、Row 模式和Mixed 模式,本文將給大家介紹如何基于Spring監(jiān)聽(tīng)Binlog日志,需要的朋友可以參考下2024-09-09零基礎(chǔ)寫(xiě)Java知乎爬蟲(chóng)之獲取知乎編輯推薦內(nèi)容
上篇文章我們拿百度首頁(yè)做了個(gè)小測(cè)試,今天我們來(lái)個(gè)復(fù)雜的,直接抓取知乎編輯推薦的內(nèi)容,小伙伴們可算松了口氣,終于進(jìn)入正題了,哈哈。2014-11-11Java線(xiàn)程并發(fā)訪(fǎng)問(wèn)代碼分析
這篇文章主要介紹了Java線(xiàn)程并發(fā)訪(fǎng)問(wèn)代碼分析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11