Topic: 替换String中某一段字符的API

  Print this page

1.替换String中某一段字符的API Copy to clipboard
Posted by: jfml
Posted on: 2003-04-19 13:41

先说这个论坛的BUG吧,我那个帖子回复不了了,每次回复都说这个帖子已经发布过一次了,真奇怪

  public static String replaceSubString(
    String parent,
    String oldSub,
    String newSub)
  {
    int sp = 0;
    int ep = 0;

    for (int i = 0; i < parent.length(); i++)
    {
      if (parent.regionMatches(i, oldSub, 0, oldSub.length()))
      {
        sp = i;
        ep = i + oldSub.length();
        break;
      }
    }
    
    StringBuffer sbParent = new StringBuffer(parent);
    sbParent.replace(sp, ep, newSub);

    return sbParent.toString();
  }

2.Re:替换String中某一段字符的API [Re: CrazyJavar] Copy to clipboard
Posted by: Jove
Posted on: 2003-04-19 13:49

关于那个Bug,好像你改一下标题就可以了
替换String某一部分,可以用
String replace(char oldChar, char newChar)
String replaceAll(String regex, String replacement)
String replaceFirst(String regex, String replacement)

后两个方法需要j2sdk1.4支持,详见api doc

3.Re:替换String中某一段字符的API [Re: CrazyJavar] Copy to clipboard
Posted by: jfml
Posted on: 2003-04-19 13:52

因为我的平台一定要1.3.1
所以只好自己写一个了
还是谢谢楼上的朋友


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923