<a href="http://blog.csdn.net/dengxu11/article/details/6632155">http://blog.csdn.net/dengxu11/article/details/6632155</a>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
///
/// 待處理的字元串
/// 要替換的字元串中的子串
/// 用來替換toRep字元串的字元串
/// 傳回一個結果字元串
public static string StringReplace(string str, string toRep, string strRep)
{
StringBuilder sb = new StringBuilder();
int subIndex = 0, indexStrRep = 0;
for (; ; )
{
/* The string which will be replace delimiter */
string str_tmp = str.Substring(subIndex);
/* Get the first index of string which will be occurrence. */
indexStrRep = str_tmp.IndexOf(toRep);
/* Equas no character to replace*/
if (indexStrRep == -1)
{
sb.Append(str_tmp);
break;
}
else
/* insert the sub string to SB */
sb.Append(str_tmp.Substring(0, indexStrRep));
if (subIndex != str.Length - 2)
{
/* insert the delimiter "strRep" to SB */
sb.Append(strRep);
}
subIndex += indexStrRep + toRep.Length;
}
return sb.ToString();
}
/// 測試用例:"dwdawdyesdwjdao dyesj yes dwjaodjawiodayes djwaiodyesjijw"
static void Main(string[] args)
string str = "1|2+|3|4|5|";
Console.WriteLine(str);
str = StringReplace(str, "|", ",");
Console.ReadKey();
}
