最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決java.lang.StringIndexOutOfBoundsException: String index out of range: -1錯(cuò)誤問題

 更新時(shí)間:2025年03月22日 11:29:49   作者:Archie_java  
這篇文章主要介紹了解決java.lang.StringIndexOutOfBoundsException: String index out of range: -1錯(cuò)誤問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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 TreeMap升序|降序排列和按照value進(jìn)行排序的案例

    Java TreeMap升序|降序排列和按照value進(jìn)行排序的案例

    這篇文章主要介紹了Java TreeMap升序|降序排列和按照value進(jìn)行排序的案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • java中避免集合死鏈調(diào)用詳情

    java中避免集合死鏈調(diào)用詳情

    這篇文章主要介紹了java中避免集合死鏈調(diào)用,開發(fā)過程中, 一些集合 的變動(dòng)會(huì)觸發(fā)任務(wù)去 改變 其他的集合 ,為了保障任務(wù)的正確執(zhí)行,應(yīng)避免出現(xiàn)死循環(huán)調(diào)用,即對 集合之間的影響關(guān)系 進(jìn)行一些限制,下面文章就來看看這種問題的避免
    2021-09-09
  • Spring Security OAuth2 token權(quán)限隔離實(shí)例解析

    Spring Security OAuth2 token權(quán)限隔離實(shí)例解析

    這篇文章主要介紹了Spring Security OAuth2 token權(quán)限隔離實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • spring boot中內(nèi)嵌redis的使用方法示例

    spring boot中內(nèi)嵌redis的使用方法示例

    這篇文章主要給大家介紹了關(guān)于spring boot中內(nèi)嵌redis使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • 如何解決SpringBoot2.6及之后版本取消了循環(huán)依賴的支持問題

    如何解決SpringBoot2.6及之后版本取消了循環(huán)依賴的支持問題

    循環(huán)依賴指的是兩個(gè)或者多個(gè)bean之間相互依賴,形成一個(gè)閉環(huán),SpringBoot從2.6.0開始默認(rèn)不允許出現(xiàn)Bean循環(huán)引用,解決方案包括在全局配置文件設(shè)置允許循環(huán)引用存在、在SpringApplicationBuilder添加設(shè)置允許循環(huán)引用、構(gòu)造器注入
    2024-10-10
  • java向文件末尾添加內(nèi)容示例分享

    java向文件末尾添加內(nèi)容示例分享

    本文為大家提供一個(gè)java向文件末尾添加內(nèi)容的示例分享,大家參考使用吧
    2014-01-01
  • SpringBoot配置Access-Control-Allow-Origin教程

    SpringBoot配置Access-Control-Allow-Origin教程

    文章介紹了三種配置Spring Boot跨域訪問的方法:1. 使用過濾器;2. 在WebConfig配置文件中設(shè)置;3. 通過注解配置,作者分享了個(gè)人經(jīng)驗(yàn),并鼓勵(lì)讀者支持腳本之家
    2025-03-03
  • 關(guān)于mybatis一對一查詢一對多查詢遇到的問題

    關(guān)于mybatis一對一查詢一對多查詢遇到的問題

    這篇文章主要介紹了關(guān)于mybatis一對一查詢,一對多查詢遇到的錯(cuò)誤,接下來是對文章進(jìn)行操作,要求查詢?nèi)课恼?,并關(guān)聯(lián)查詢作者,文章標(biāo)簽,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • Spring Boot中使用RabbitMQ 生產(chǎn)消息和消費(fèi)消息的實(shí)例代碼

    Spring Boot中使用RabbitMQ 生產(chǎn)消息和消費(fèi)消息的實(shí)例代碼

    本文介紹了在SpringBoot中如何使用RabbitMQ進(jìn)行消息的生產(chǎn)和消費(fèi),詳細(xì)闡述了RabbitMQ中交換機(jī)的作用和類型,包括直連交換機(jī)、主題交換機(jī)、扇出交換機(jī)和頭交換機(jī),并解釋了各自的消息路由機(jī)制,感興趣的朋友一起看看吧
    2024-10-10
  • 使用java?實(shí)現(xiàn)mqtt兩種常用方式

    使用java?實(shí)現(xiàn)mqtt兩種常用方式

    在開發(fā)MQTT時(shí)有兩種方式一種是使用Paho Java 原生庫來完成,一種是使用spring boot 來完成,這篇文章主要介紹了使用java?實(shí)現(xiàn)mqtt兩種方式,需要的朋友可以參考下
    2022-11-11

最新評論

噶尔县| 白沙| 探索| 莱州市| 嵩明县| 蓬溪县| 西林县| 临高县| 凤庆县| 小金县| 临高县| 长乐市| 务川| 太湖县| 汉阴县| 遂宁市| 章丘市| 贵州省| 唐河县| 陵水| 达拉特旗| 遂平县| 娱乐| 汝阳县| 洛扎县| 鹿泉市| 饶阳县| 章丘市| 曲阳县| 如皋市| 永兴县| 平乐县| 晋宁县| 抚顺市| 吴旗县| 万州区| 隆子县| 枣阳市| 永昌县| 青冈县| 郧西县|