
从cc++转到java需要点时间
先上个只删除一个的代码
package test;
import java.math.*;
import java.util.*;
public class test1 {//一个文件中只能有一个public类
public static void main (String[] args) {
String s1 = " this a my exemple ";
String s2 = "my";
if(s1.startsWith(s2)){
s1 = s1.substring(s2.length());
}else if(s1.endsWith(s2)){
s1 = s1.substring(0, s1.length()-s2.length());
}else {
int index = s1.indexOf(s2);
if(index!=-1){
String s11 = s1.substring(0, index);
String s22 = s1.substring(index+s2.length());
s1 = s11 + s22;
}
}
System.out.println(s1);
}
}
万一要删除多个呢?
package test;
import java.math.*;
import java.util.*;
public class test1 {//一个文件中只能有一个public类
public static void main (String[] args) {
String s1 = " my this a my exemple my ";
String s2 = "my";
int index = s1.indexOf(s2);
while(index != -1){
String s11 = s1.substring(0, index);
String s22 = s1.substring(index + s2.length());
s1 = s11 + s22;
index = s1.indexOf(s2);
}
System.out.println(s1);
}
}
其实挺简单的




近期评论