使用String类的indexOf()和subString方法犯了一个低级的错误
2013-09-15 00:00
603 查看
今天在公司开发,在使用String类的字符串操作函数的时候犯了一个比较低级的错误。原因是自己对substring()方法以及indexOf()方法的使用了解的不够深入。
错误的代码如下:StringfrpName=frpName.substring(0,frpName.indexOf("("));用处是:当字符串后面包含(),就将()去掉。因为少判断了indexOf("(")等于-1的情况,所以程序编译没有出错,却在运行的时候导致系统出现了问题。
两个函数的源代码如下:
publicintindexOf(Stringstr,intfromIndex){
returnindexOf(value,offset,count,
str.value,str.offset,str.count,fromIndex);
}
//source源字符,sourceOffset源偏移,sourceCount源长度
//target查找的字符...
staticintindexOf(char[]source,intsourceOffset,intsourceCount,
char[]target,inttargetOffset,inttargetCount,
intfromIndex){
//如果fromIndex比源字符还长(从0算起),并且查找的字符长度为0,那就返回源字符的长度,否则返回-1
if(fromIndex>=sourceCount){
return(targetCount==0?sourceCount:-1);
}
if(fromIndex<0){
fromIndex=0;
}
//如果fromIndex比源字符短,查找的字符长度为0,直接返回fromIndex
if(targetCount==0){
returnfromIndex;
}
//先取出第一个字符
charfirst=target[targetOffset];
intmax=sourceOffset+(sourceCount-targetCount);
//循环每一个字符
for(inti=sourceOffset+fromIndex;i<=max;i++){
/*直到找到第一个字符*/
if(source[i]!=first){
while(++i<=max&&source[i]!=first);
}
/*找到第一个字符后,比较剩下的字符*/
if(i<=max){
intj=i+1;
intend=j+targetCount-1;
for(intk=targetOffset+1;j<end&&source[j]==
target[k];j++,k++);
if(j==end){
/*如果j能到end,那就说明找到整个字符串啦,返回偏移*/
returni-sourceOffset;
}
}
}
return-1;
}
/**
*Returnsanewstringthatisasubstringofthisstring.The
*substringbeginsatthespecified<code>beginIndex</code>and
*extendstothecharacteratindex<code>endIndex-1</code>.
*Thusthelengthofthesubstringis<code>endIndex-beginIndex</code>.
*<p>
*Examples:
*<blockquote><pre>
*"hamburger".substring(4,8)returns"urge"
*"smiles".substring(1,5)returns"mile"
*</pre></blockquote>
*
*@parambeginIndexthebeginningindex,inclusive.
*@paramendIndextheendingindex,exclusive.
*@returnthespecifiedsubstring.
*@exceptionIndexOutOfBoundsExceptionifthe
*<code>beginIndex</code>isnegative,or
*<code>endIndex</code>islargerthanthelengthof
*this<code>String</code>object,or
*<code>beginIndex</code>islargerthan
*<code>endIndex</code>.
*/
publicStringsubstring(intbeginIndex,intendIndex){
if(beginIndex<0){
thrownewStringIndexOutOfBoundsException(beginIndex);
}
if(endIndex>count){
thrownewStringIndexOutOfBoundsException(endIndex);
}
if(beginIndex>endIndex){
thrownewStringIndexOutOfBoundsException(endIndex-beginIndex);
}
return((beginIndex==0)&&(endIndex==count))?this:
newString(offset+beginIndex,endIndex-beginIndex,value);
}
错误的代码如下:StringfrpName=frpName.substring(0,frpName.indexOf("("));用处是:当字符串后面包含(),就将()去掉。因为少判断了indexOf("(")等于-1的情况,所以程序编译没有出错,却在运行的时候导致系统出现了问题。
两个函数的源代码如下:
*Returnsanewstringthatisasubstringofthisstring.The
*substringbeginsatthespecified<code>beginIndex</code>and
*extendstothecharacteratindex<code>endIndex-1</code>.
*Thusthelengthofthesubstringis<code>endIndex-beginIndex</code>.
*<p>
*Examples:
*<blockquote><pre>
*"hamburger".substring(4,8)returns"urge"
*"smiles".substring(1,5)returns"mile"
*</pre></blockquote>
*
*@parambeginIndexthebeginningindex,inclusive.
*@paramendIndextheendingindex,exclusive.
*@returnthespecifiedsubstring.
*@exceptionIndexOutOfBoundsExceptionifthe
*<code>beginIndex</code>isnegative,or
*<code>endIndex</code>islargerthanthelengthof
*this<code>String</code>object,or
*<code>beginIndex</code>islargerthan
*<code>endIndex</code>.
*/
publicStringsubstring(intbeginIndex,intendIndex){
if(beginIndex<0){
thrownewStringIndexOutOfBoundsException(beginIndex);
}
if(endIndex>count){
thrownewStringIndexOutOfBoundsException(endIndex);
}
if(beginIndex>endIndex){
thrownewStringIndexOutOfBoundsException(endIndex-beginIndex);
}
return((beginIndex==0)&&(endIndex==count))?this:
newString(offset+beginIndex,endIndex-beginIndex,value);
}
相关文章推荐
- 转:C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
- C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
- 一个关于WCF调用远程链接返回405错误不允许使用此方法的问题
- C#中String类 --> "检索","截取"等几个方法(IndexOf、LastIndexOf、Substring)
- 网站后台登录aspcms 提示错误号:-2147467259,错误描述:操作必须使用一个可更新的查询。sql=update AspCms_Content set TimeStatus=0 where TimeStatus=1 and Timeing <= 解决方法。
- 一个HTML的低级错误!!!与不能同时使用
- Object-c学习点滴——使用NSMutableArray犯的一个低级错误!!
- C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
- C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
- "操作必须使用一个可更新的查询"错误的解决方法
- C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
- js参数使用时常犯的一个低级错误
- java的String类中的其中一个subString方法的源码
- Java的String类的subString()方法使用方法
- C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
- c#--怎样使用c#中的indexof和substring方法
- C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
- 关于COM接口使用时候的一个低级错误
- 【Java面试题】17 如何把一个逗号分隔的字符串转换为数组? 关于String类中split方法的使用,超级详细!!!
- C#中String类的几个方法(IndexOf、LastIndexOf、Substring)