我用String.indexOf(string)获取字符串时遇到的问题? 可以考虑用try ...catch 子句捕捉一下错误,然后不做任何处理就行了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 首先,还是要看看错误发生的原因,严谨些嘛! 一般string容易出StringIndexOutOfBoundsException的问题,往往在循环等中出现。如果非要忽略,就这样try { String.indexOf(string);}catch(Exception e){ System.out.println(e.toString());}^^^^^^^这样就可以忽略了 if(str1.indexOf(searchStr) == -1) { // not found ...}else { ...} 我写了个小程序测试了一下,好像并不会中断程序。程序如下:package com.CSDN.String;public class TestString { public static void main(String[] args) { String testString = "i love java"; int returnValue = testString.indexOf("aaa"); System.out.println(returnValue); returnValue = testString.indexOf("a"); System.out.println(returnValue); }}运行结果:-18 帮忙解决一下问题,用java编写此程序 关于静态成员类和静态成员接口的问题,请指教 如何有效去除StringBuffer中字符串的最后一个非空字符???? 非常简单的java题目 !!帮帮忙~~!! 散分啦,大家进来,我想问问有关培训的事情!!!! applet显示问题 一个初级问题 书上例子不解? 新手上路,debug的问题. java题求解 急急...送分。。关于JSP的错误:java.lang.StringIndexOutOfBoundsException: String index out of range: -1?? 请问哪儿有JDK1.4的比较好的文档
一般string容易出StringIndexOutOfBoundsException的问题,往往在循环等中出现。如果非要忽略,就这样
try {
String.indexOf(string);
}
catch(Exception e){
System.out.println(e.toString());
}^^^^^^^
这样就可以忽略了
...
}else {
...
}
程序如下:
package com.CSDN.String;public class TestString {
public static void main(String[] args) {
String testString = "i love java";
int returnValue = testString.indexOf("aaa");
System.out.println(returnValue);
returnValue = testString.indexOf("a");
System.out.println(returnValue);
}
}运行结果:
-1
8