初学者问题:如何实现字符串的替换

zl198183 2004-11-11 01:12:28
class MyUtil {
String replaceString
(String original, String replacement){}
}
...全文
144 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zl198183 2004-11-11
  • 打赏
  • 举报
回复
如果不用已有的REPLACE()方法,还有别的方法吗?
replaceall()方法是如何具体实现的??
caiyi0903 2004-11-11
  • 打赏
  • 举报
回复
一百遍啊一百遍!!
御南 2004-11-11
  • 打赏
  • 举报
回复
用JTextArea的replaceRange(textString,int start,int end)方法
classjava 2004-11-11
  • 打赏
  • 举报
回复
public class Test
{
public static void main(String[] args)
{
String s="csdn.net";
String sChange=s.replaceAll("csdn","classjava");
System.out.println(sChange);
}
}
这是String类的一个method
String replaceAll(String s1,string s2)
treeroot 2004-11-11
  • 打赏
  • 举报
回复
用String类的replaceAll()方法
bestdelphier 2004-11-11
  • 打赏
  • 举报
回复
/**
* 将source字符串中的replaceWith字符串替换成replaceTo字符串
* @param source String 源字符串
* @param replaceWith String源字符串中待替换的字串
* @param repalceTo String源字符串中字串欲替换成的字串
* @return java.lang.String 替换之后的字符串
*/
public static String replaceString(String source, String replaceWith, String replaceTo)
{
StringBuffer sb=new StringBuffer();
int iLength=replaceWith.length();
int iLastPos=0,iFindPos=0;
iFindPos=source.indexOf(replaceWith,0);
while (iFindPos!=-1){
sb.append(source.substring(iLastPos,iFindPos));
sb.append(replaceTo);
iLastPos=iFindPos+iLength;
iFindPos=source.indexOf(replaceWith,iLastPos);
}
sb.append(source.substring(iLastPos,source.length()));
return sb.toString();
}
treeroot 2004-11-11
  • 打赏
  • 举报
回复
String类就有这个方法

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧