您的位置:首页 > 其它

使用substring()方法完成字符串替换.

2006-06-09 13:38 435 查看
/*

利用substring()方法可以截取字符串,它有两种形式

String substring(int startIndex)

String substring(int startIndex, int endIndex)

*/

//Substring replacement.
public class StringReplace{
public static void main(String[] args)
{
String org = "This is a test. This is, too.";
String search = "is";
String sub = "was";
String result = "";
int i;
do{ // replace all matching substrngs
System.out.println(org);
i = org.indexOf(search);
if(i !=-1){
result = org.substring(0,i);
result = result + sub;
result = result + org.substring(i + search.length());
org = result;
}
}while(i != -1);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐