String index out of range: -1

原文地址为: String index out of range: -1

      前两天报字符串越界,查找中发现,应该是取某一个字符的位置时,出错了,原来使用lastIndexOf时要取得这个字符在被查找的字符串里没有。

解决方法,在取位置之前,先要验证一下,字符是否存在。

 if(name.lastIndexOf("Form")!=-1){

      name = name.substring(0, name.lastIndexOf("Form"));

}

JScript  语言参考

 

--------------------------------------------------------------------------------

lastIndexOf 方法
返回 String 对象中子字符串最后出现的位置。

strObj.lastIndexOf(substring[, startindex])

参数
strObj

必选项。String 对象或文字。

substring

必选项。要在 String 对象内查找的子字符串。

startindex

可选项。该整数值指出在 String 对象内进行查找的开始索引位置。如果省略,则查找从字符串的末尾开始。

说明
lastIndexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。

如果 startindex 是负数,则 startindex 被当作零。如果它比最大字符位置索引还大,则它被当作最大的可能索引。

从右向左执行查找。否则,该方法和 indexOf 相同。

下面的示例说明了 lastIndexOf 方法的用法:

function lastIndexDemo(str2)
{
   var str1 = "BABEBIBOBUBABEBIBOBU"
   var s = str1.lastIndexOf(str2);
   return(s);
}

在java类中使用,大体上差不多。

 以下摘自http://www.chinageren.com/jc/HTML/115914_2.html

DateBean.java
{
   private String dateStr;
   private String year;
   private String month;
   private String day;
   //
   public void setDateStr(String str)    //私有变量dateStr的set方法
   {
     this.dateStr=str;
   }
   public String getDateStr()    //私有变量dateStr的get方法
   {
     return dateStr;
   }
   public String getYear()//得到年的字符串
   {
     int a=dateStr.indexOf("-");//求第一个“-”的位数
     year=dateStr.substring(0,a);//取第一个“-”前的字符串
     return year;
   }
   public String getMonth()//得到月的字符串
   {
     int a=dateStr.indexOf("-");//求第一个“-”的位数
     int b=dateStr.lastIndexOf("-");//求最后一个“-”的位数
     month=dateStr.substring(a+1,b);//取两个“-”之间的字符串
     return month;
   }
   public String getDay()//得到日的字符串
   {
     int b=dateStr.lastIndexOf("-");//求最后一个“-”的位数
     int len=dateStr.length();//求字符串的长度
     day=dateStr.substring(b+1,len);//取最后一个“-”以后的字符串
     return day;
   }
}

转载请注明本文地址: String index out of range: -1
这个错误出现在字符串的索引超出范围时。在引用中,错误消息为"String index out of range: 8000",而在你的问题中,错误消息为"String index out of range: -1"。这个错误通常发生在通过索引访问字符串时,索引超出了字符串的长度范围。 在引用中,提供了一个Java程序示例。该程序读取一个文件的内容,并按照特定的规则对内容进行排序,然后将排序后的内容写入到另一个文件中。在这个示例中,没有看到任何与字符串索引相关的操作,因此无法直接解释你提供的错误消息。 为了更好地帮助你解决问题,请提供更多的上下文信息,包括你遇到该错误的具体代码和相关的输入数据。这将有助于我更准确地分析和解释你遇到的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [MyBatis使用Mapper查询时报错:java.lang.StringIndexOutOfBoundsException: String index out of range: ...](https://blog.csdn.net/libertine1993/article/details/80245005)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [java.lang.StringIndexOutOfBoundsException: String index out of range: -1](https://blog.csdn.net/weixin_46113448/article/details/125354541)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值