最近在处理一些字符串的替换中发生的一些问题。
最近需要临时实现一个再FCK中替换一些字符串的工作。
因此首先想到的的一个方法就是replaceAll();方法。这个简单也很实在。
故第一次实现的伪代码段位:
int index = description.indexOf("XXXX");
if(index != -1){
description = description.replaceAll("XXXX","BBBB");
}
//....将Description更新。
这段代码测试的时候,没有问题。但是杯具还是发生了。因为replaceAll属于regex的替换。在替换的时候如果碰到一些特殊字符就会出现Exception。 比如"<!--adsafads -->","*"。。。
没办法哥只能想其他的办法,接着就使用了java的subString来进行处理
第二次实现的伪代码为:
String result="";
String replaceDesc="DDDD";
do{
index = description.indexOf("XXXX");
if(index !=-1){
result= description.substring(0,index);
result= result+replaceDesc;
result= result+description.substring(index+desc.length());
description = result;
}
}while(index !=-1);
哥想这下应该能够解决了吧!将特殊的一些字符串测试了下。通过了。
但是问题又出现了,由于用户是在Windows系统下进行操作的,而服务器为Liunx。那么在一些特殊的换行或者其他的时候,就会发现系统之前的一些却别比如:换行\r 和\r\n的区别。
因此如果在使用对FCK中的HTML中的默些字符串进行匹配替换或者删除的时候,使用indexOf()并不是最好的选择,尤其是主要使用者的系统和服务器的系统的一些区别!! 同时也可以参考htmlParser来进行对FCK中的HTML处理。