萍聚社区-德国热线-德国实用信息网

 找回密码
 注册

微信登录

微信扫一扫,快速登录

萍聚头条

查看: 561|回复: 0

1-1-19-7 如何替换字符串里的子串??

[复制链接]
发表于 2003-2-7 01:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册 微信登录

x

  1. 发信人: huzq (浪人), 信区: Java      
  2. 标  题: Re: 如何替换字符串里的子串??
  3. 发信站: BBS 水木清华站 (Fri May 24 21:56:27 2002)

  4. 给你我写的一个函数

  5. //字符串替换函数
  6. public static String Replace(String strSource, String strOld, String strNew){

  7.     int index, indexStart;
  8.     StringBuffer strDest = new StringBuffer(0);
  9.     int nOldLength = strOld.length();
  10.     if(nOldLength == 0){
  11.         strDest.append(strSource);
  12.         return strDest.toString();
  13.     }
  14.     index = strSource.indexOf(strOld, 0);
  15.     if(index == -1){
  16.         strDest.append(strSource);
  17.         return strDest.toString();
  18.     }
  19.     strDest.append(strSource.substring(0, index));
  20.     strDest.append(strNew);
  21.     indexStart = index+nOldLength;
  22.     while((index = strSource.indexOf(strOld, indexStart)) != -1){
  23.         strDest.append(strSource.substring(indexStart, index));
  24.         strDest.append(strNew);
  25.         indexStart = index + nOldLength;
  26.     }
  27.     if (indexStart < strSource.length() - 1)
  28.         strDest.append(strSource.substring(indexStart));
  29.     return strDest.toString();
  30. }

  31. 【 在 bluejoe 的大作中提到: 】
  32. : String.Replace只能换字符啊

复制代码
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
您需要登录后才可以回帖 登录 | 注册 微信登录

本版积分规则

手机版|Archiver|AGB|Impressum|Datenschutzerklärung|萍聚社区-德国热线-德国实用信息网

GMT+1, 2025-2-7 13:18 , Processed in 0.070351 second(s), 16 queries , MemCached On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表