字符串替换问题如果有下面一个字符串为:String s1 = 2007年11月在koders.com搜索代码时,发现*.erl格式的源文件,感叹开发语言的花样百出,此时,我觉得erlang是一个丑陋的小家伙,看名字就没有对它提起多少兴趣。
如果字符串“搜索代码”, “花样百出” ,“丑陋的小家伙” 为非法字符, 现想过滤,List blockList = new ArrayList();
blockList.add("搜索代码");
blockList.add("花样百出");
blockList.add("丑陋的小家伙");
现想把s1变成下面这样:
String s1 = 2007年11月在koders.com****时,发现*.erl格式的源文件,感叹开发语言的****,此时,我觉得erlang是一个****,看名字就没有对它提起多少兴趣。
怎么用程序解决?
如果字符串“搜索代码”, “花样百出” ,“丑陋的小家伙” 为非法字符, 现想过滤,List blockList = new ArrayList();
blockList.add("搜索代码");
blockList.add("花样百出");
blockList.add("丑陋的小家伙");
现想把s1变成下面这样:
String s1 = 2007年11月在koders.com****时,发现*.erl格式的源文件,感叹开发语言的****,此时,我觉得erlang是一个****,看名字就没有对它提起多少兴趣。
怎么用程序解决?
String s1 = "2007年11月在koders.com搜索代码时,发现*.erl格式的源文件,感叹开发语言的花样百出,此时,我觉得erlang是一个丑陋的小家伙,看名字就没有对它提起多少兴趣。"List blockList = new ArrayList();
blockList.add("搜索代码");
blockList.add("花样百出");
blockList.add("丑陋的小家伙"); String mask="****";
for(int i=0; i<blockList.size();i++){
s1.replaceAll((String)blockList.get(i),mask);
}
String s1 = "2007年11月在koders.com搜索代码时,发现*.erl格式的源文件,感叹开发语言的花样百出,此时,我觉得erlang是一个丑陋的小家伙,看名字就没有对它提起多少兴趣。"List blockList = new ArrayList();
blockList.add("搜索代码");
blockList.add("花样百出");
blockList.add("丑陋的小家伙"); String mask="****";
for(int i=0; i<blockList.size();i++){
s1=s1.replaceAll((String)blockList.get(i),mask);
}
}试试!很久没做过这东东了!
List blockList = new ArrayList();
blockList.add("搜索代码");
blockList.add("花样百出");
blockList.add("丑陋的小家伙");
for(int i=0; i<blockList.size();i++){
String block = (String)blockList.get(i);
s1=s1.replaceAll(block , getStars (block.length) );
}//定义一个函数
String getStars(int length)
{
String stars="";
for(int i=0;i<length;i++) stars+="*";
}
blockList.add("搜索代码");
blockList.add("花样百出");
blockList.add("丑陋的小家伙"); 如果字符串长度为4, 则有四个*
如果字符串长度为6, 则有六个*import java.util.ArrayList;
import java.util.List;public class TestMain { /**
* @param args
*/
public static void main(String[] args) {
String s1 = "2007年11月在koders.com搜索代码时,发现*.erl格式的源文件,感叹开发语言的花样百出,此时,我觉得erlang是一个丑陋的小家伙,看名字就没有对它提起多少兴趣"; List blockList = new ArrayList();
blockList.add("搜索代码");
blockList.add("花样百出");
blockList.add("丑陋的小家伙"); StringBuffer sb = new StringBuffer(s1);
for (int i = 0; i < blockList.size(); i++) {
String blockString = (String)blockList.get(i);
int index = s1.indexOf(blockString);
String s3 = "";
for (int j = 0; j < blockString.length(); j++) {
s3 += "*";
}
sb.replace(index, index + blockString.length(), s3); }
System.out.println(sb); }}
public class StrFilter { public static void main(String[] args)
{
String s1 = "2007年11月在koders.c om搜索代码时,发现*.erl格式的源文件,感叹开发语言的花样百出,此时,我觉得erlang是一个丑陋的小家伙,看名字就没有对它提起多少兴趣。";
System.out.println(filter(s1));
}
public static String filter(String str)
{
List blockList = new ArrayList();
blockList.add("搜索代码");
blockList.add("花样百出");
blockList.add("丑陋的小家伙");
String getStr = null;
Iterator it = blockList.iterator();
while(it.hasNext())
{
getStr = (String)it.next();
str = str.replaceAll(getStr, implicit(getStr.length()));
}
return str;
}
public static String implicit(int amount)
{
StringBuffer implicit = new StringBuffer("*");
for(int i=0; i<amount-1; i++)
implicit.append("*");
return implicit.toString();
}
}
本人很菜,写得不好的,还请大家指教!