http://blog.csdn.net/dengxu11/article/details/6632155
-
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.WriteLine(str);
-
Console.ReadKey();
-
}
-
}
阅读(2896) | 评论(0) | 转发(0) |