2007-7-13,中午,去了一家公司面试,笔试时碰到一道题目,把字符串中的“rep”替换成“with”,因为没上机,所以没写出来,回来试了一下。
大概函数原型是
public static String replace(String test,String rep,String with){
//
String strDest = "";
int intFromLen = rep.length();
int intPos;
while((intPos=test.indexOf(rep))!=-1){
strDest += test.substring(0,intPos);
strDest += with;
test = test.substring(intPos+intFromLen);
}
strDest += test;
return strDest;
}
大概函数原型是
public static String replace(String test,String rep,String with){
//
String strDest = "";
int intFromLen = rep.length();
int intPos;
while((intPos=test.indexOf(rep))!=-1){
strDest += test.substring(0,intPos);
strDest += with;
test = test.substring(intPos+intFromLen);
}
strDest += test;
return strDest;
}
本文介绍了一个简单的字符串替换函数实现方法,该方法通过遍历输入字符串并查找特定子串来进行替换操作。

919

被折叠的 条评论
为什么被折叠?



