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;
}