Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12876604
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: C#/.net

2013-05-27 10:46:48

http://blog.csdn.net/dengxu11/article/details/6632155

 


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. class Program
  6. {
  7.     ///
  8.     ///
  9.     ///
  10.     /// 待处理的字符串
  11.     /// 要替换的字符串中的子串
  12.     /// 用来替换toRep字符串的字符串
  13.     /// 返回一个结果字符串
  14.     public static string StringReplace(string str, string toRep, string strRep)
  15.     {
  16.         StringBuilder sb = new StringBuilder();
  17.         int subIndex = 0, indexStrRep = 0;
  18.         for (; ; )
  19.         {
  20.             /* The string which will be replace delimiter */
  21.             string str_tmp = str.Substring(subIndex);
  22.             /* Get the first index of string which will be occurrence. */
  23.             indexStrRep = str_tmp.IndexOf(toRep);
  24.             /* Equas no character to replace*/
  25.             if (indexStrRep == -1)
  26.             {
  27.                 sb.Append(str_tmp);
  28.                 break;
  29.             }
  30.             else
  31.             {
  32.                 /* insert the sub string to SB */
  33.                 sb.Append(str_tmp.Substring(0, indexStrRep));
  34.                 if (subIndex != str.Length - 2)
  35.                 {
  36.                     /* insert the delimiter "strRep" to SB */
  37.                     sb.Append(strRep);
  38.                 }
  39.                 subIndex += indexStrRep + toRep.Length;
  40.             }
  41.         }
  42.         return sb.ToString();
  43.     }

  44.     ///
  45.     /// 测试用例:"dwdawdyesdwjdao dyesj yes dwjaodjawiodayes djwaiodyesjijw"
  46.     ///
  47.     ///
  48.     static void Main(string[] args)
  49.     {
  50.         string str = "1|2+|3|4|5|";
  51.         Console.WriteLine(str);
  52.         str = StringReplace(str, "|", ",");
  53.         Console.WriteLine(str);
  54.         Console.ReadKey();
  55.     }
  56. }



image

阅读(2899) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~