public static int replacer(String oldS,String newS)
{
int num = 0;
for(int i=0;i<oldS.length();i++) {
int check = oldS.indexOf(newS,i);
if(check != -1) {
i = check;
num ++;
}else {
break;
}
}
return num;
}
{
int num = 0;
for(int i=0;i<oldS.length();i++) {
int check = oldS.indexOf(newS,i);
if(check != -1) {
i = check;
num ++;
}else {
break;
}
}
return num;
}
解决方案 »
- exception in thread "main" java.lang.NoSuchMethodError:main 问题
- 多态俺懂了,强制类型转换又晕了,大侠指点迷经啊~~~~~
- 在JAVA里怎样与数据库联系
- JRE环境下是否需要将JDK或JDK以外的包配置上?
- 请问哪些类可以被实例化
- 怎么样将double数据转成String型,且String变量不会显视科学计数法?
- 如何打开新窗口
- 关于如何处理时间的问题
- StringBuffer的append方法可以改变自己的内容,除此之外没有方法可以改变自己的内容了吧?
- 哪里有中文的JDK下载?急!在线等待!
- 为什么在win200下我用jb7写applet在网页上显示不了???
- 各位高手救命啊
用JDK1.4,String自带有此替换方法,听说的
public static int getReplace(StringBuffer sb, String strSrc, String strDst) {
// do some pretreatment firstly
// i.e. when strSrc is NULL or is empty
String str = sb.toString();
StringBuffer sbReturn = new StringBuffer();
int i, curr = 0, count = 0, len = strSrc.length();
while ((i = str.indexOf(strSrc, curr)) != -1) {
sbReturn.append(str.substring(curr, i));
sbReturn.append(strDst);
curr = i + len;
count++;
}
sb.replace(0, sb.length(), sbReturn.toString());
return count;
}
class test
{
public static void main(String[] args)
{
String reStr = "this is a back and is all";
System.out.println(reStr);
reStr = reStr.replaceAll("is","as");
System.out.println(reStr);
}
}然后就是求替换多少次了class test
{
public static void main(String[] args)
{
String reStr = "this is a back and is all";
String re = "is";
String tempStr =reStr;
int i = 0;
int count = 0;
do
{
i = tempStr.indexOf(re); if(i==-1)
{ }
else
{
count++;
tempStr = tempStr.substring(i+1);
}
}
while(i!=-1);
System.out.println(count);
System.out.println(reStr);
reStr = reStr.replaceAll("is","as");
System.out.println(reStr); }
}
int 方法(全文,老字串,新字串)^_^,不好意思.
whiteshen(White Shen)的方法 调试中.....
// do some pretreatment firstly
// i.e. when strSrc is NULL or is empty
String str = sb.toString();
StringBuffer sbReturn = new StringBuffer();
int i, curr = 0, count = 0, len = strSrc.length();
while ((i = str.indexOf(strSrc, curr)) != -1) {
sbReturn.append(str.substring(curr, i));
sbReturn.append(strDst);
curr = i + len;
count++;
}
sbReturn.append(str.substring(curr,str.length())); /**/
sb.replace(0, sb.length(), sbReturn.toString());
return count;
}}
好了,没问题了
谢谢