字符串的删除 和替换为指定字符串

import java.util.Random;

public class testarray {
static Random rand = new Random();
public static void main(String[] args) {
String a="acb<cdd id=xxx> ddddd</cdd> <cdd id=addddd> ddddd</cdd>";
String start1=" <cdd id=addddd> ";
String start=start1.trim();
System.out.println(start);
String end="</cdd>";
System.out.println(a);
String aa=replaceString(a,start,end);
System.out.println(aa);
String replace=rand.toString();
String ab=replaceStringToString(a,start,end,replace);
System.out.println(ab);

}
//把指定字符串替换为空 mainString 指定字符串 needStartReplaceString 删除字符串的前缀
needEndReplaceString 删除字符串的后缀
public static String replaceString(String mainStrng,String needStartReplaceString,String needEndReplaceString){
int start =mainStrng.lastIndexOf(needStartReplaceString);
int end=mainStrng.indexOf(needEndReplaceString, start)+needEndReplaceString.length();
String sub=mainStrng.substring(start,end );
String ret=null;
ret=mainStrng.replace(sub, " ");
return ret;
}
  //把指定字符串替换为指定字符串 mainString 指定字符串 needStartReplaceString 替换字符串的前缀
needEndReplaceString 替换字符串的后缀
replaceString 替换为指定字符串
public static String replaceStringToString(String mainStrng,String needStartReplaceString,String needEndReplaceString,String replaceString){
int start =mainStrng.lastIndexOf(needStartReplaceString);
int end=mainStrng.indexOf(needEndReplaceString, start)+needEndReplaceString.length();
String sub=mainStrng.substring(start,end );
String ret=null;
ret=mainStrng.replace(sub,replaceString);
return ret;
}


}

你可能感兴趣的:(字符串)