当前位置: 技术问答>java相关
如何实现字符串型字符的替换?
来源: 互联网 发布时间:2015-07-06
本文导语: 例如:把路径:c:34455667.txt 中的“”换成"\"?怎么实现? 多谢 在线给分 | jsp中没有replace函数,自定义一个! = 0 ) { char [] line2 = line.toCharArray(); char []...
例如:把路径:c:34455667.txt 中的“”换成"\"?怎么实现?
多谢
在线给分
多谢
在线给分
|
jsp中没有replace函数,自定义一个!
= 0 ) {
char [] line2 = line.toCharArray();
char [] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while( ( i=line.indexOf( oldString, i ) ) > 0 ) {
buf.append(line2, j, i-j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
//如果在line中没有oldString 返回line
return line;
}
/**
* 做不区分大小写的模式匹配,并用newString 来替换 oldString
*
* @param line 原字符串
* @param oldString 被替换的字符串
* @param newString 新的要替换oldString的字符串
*
* @return 返回所有oldString都被newString替换的字符串
*/
public static final String replaceIgnoreCase(String line, String oldString,
String newString)
{
//如果line是null,直接返回
if (line == null) {
return null;
}
String lcLine = line.toLowerCase();
String lcOldString = oldString.toLowerCase();
int i=0;
if ( ( i=lcLine.indexOf( lcOldString, i ) ) >= 0 ) {
char [] line2 = line.toCharArray();
char [] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while( ( i=lcLine.indexOf( lcOldString, i ) ) > 0 ) {
buf.append(line2, j, i-j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
/**
* 用 newString 替换 line 中的所有的 OldString
* count返回被替换的数目
*
* @param line 原字符串
* @param oldString 被替换的字符串
* @param newString 新的要替换oldString的字符串
*
* @return 返回所有oldString都被newString替换的字符串
*/
public static final String replace( String line, String oldString,
String newString, int[] count)
{
if (line == null) {
return null;
}
int i=0;
if ( ( i=line.indexOf( oldString, i ) ) >= 0 ) {
int counter = 0;
counter++;
char [] line2 = line.toCharArray();
char [] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while( ( i=line.indexOf( oldString, i ) ) > 0 ) {
counter++;
buf.append(line2, j, i-j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
count[0] = counter;
return buf.toString();
}
return line;
}
= 0 ) {
char [] line2 = line.toCharArray();
char [] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while( ( i=line.indexOf( oldString, i ) ) > 0 ) {
buf.append(line2, j, i-j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
//如果在line中没有oldString 返回line
return line;
}
/**
* 做不区分大小写的模式匹配,并用newString 来替换 oldString
*
* @param line 原字符串
* @param oldString 被替换的字符串
* @param newString 新的要替换oldString的字符串
*
* @return 返回所有oldString都被newString替换的字符串
*/
public static final String replaceIgnoreCase(String line, String oldString,
String newString)
{
//如果line是null,直接返回
if (line == null) {
return null;
}
String lcLine = line.toLowerCase();
String lcOldString = oldString.toLowerCase();
int i=0;
if ( ( i=lcLine.indexOf( lcOldString, i ) ) >= 0 ) {
char [] line2 = line.toCharArray();
char [] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while( ( i=lcLine.indexOf( lcOldString, i ) ) > 0 ) {
buf.append(line2, j, i-j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
/**
* 用 newString 替换 line 中的所有的 OldString
* count返回被替换的数目
*
* @param line 原字符串
* @param oldString 被替换的字符串
* @param newString 新的要替换oldString的字符串
*
* @return 返回所有oldString都被newString替换的字符串
*/
public static final String replace( String line, String oldString,
String newString, int[] count)
{
if (line == null) {
return null;
}
int i=0;
if ( ( i=line.indexOf( oldString, i ) ) >= 0 ) {
int counter = 0;
counter++;
char [] line2 = line.toCharArray();
char [] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while( ( i=line.indexOf( oldString, i ) ) > 0 ) {
counter++;
buf.append(line2, j, i-j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
count[0] = counter;
return buf.toString();
}
return line;
}
|
因为""是特殊字符,所以再所有出现""的地方,都必须进行转意而使用"\"
|
用我的函数
replace(strToReplace,"\","\\");
一定可以的。
replace(strToReplace,"\","\\");
一定可以的。