1:  class Program
   2:      {
   3:          static void Main(string[] args)
   4:          {
   5:              string s = File.ReadAllText(@"e:\test.txt");
   6:              Program p = new Program();
   7:              string r6 = p.Replace_1(s, ' ', 'a');
   8:              string r7 = p.Replace_2(s, " ", "a");
   9:              string r1=p.ReplaceEmptyChar_1(s, "a");
  10:              string r2 = p.ReplaceEmptyChar_2(s, "a");
  11:              string r3 = p.ReplaceEmptyChar_3(s, "a");
  12:              string r4 = p.ReplaceEmptyChar_4(s, "a");
  13:              string r5 = p.ReplaceEmptyChar_5(s, 'a');
  14:              string r8 = p.ReplaceEmptyChar_6(s, 'a');
  15:              Console.WriteLine(string.Equals(r5, r7));
  16:          }
  17:   
  18:          /// <summary>
  19:          /// replace empty char with new string
  20:          /// </summary>
  21:          /// <param name="s">string to be dealed</param>
  22:          /// <param name="to_be_replaced">replace with this string</param>
  23:          /// <returns>new string</returns>
  24:          public string ReplaceEmptyChar_1(string s,string to_be_replaced)
  25:          {
  26:              if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(to_be_replaced) || to_be_replaced==" ")
  27:              {
  28:                  return s;
  29:              }
  30:   
  31:              char[] charArray = s.ToCharArray();
  32:              char[] replaceArray = to_be_replaced.ToCharArray();
  33:              int replaced_length=to_be_replaced.Length;
  34:              //get the empty count
  35:              int emptyCount = 0;
  36:              for (int i=0;i<charArray.Length;i++)
  37:              {
  38:                  if (charArray[i] == ' ')
  39:                  {
  40:                      emptyCount++;
  41:                      if (replaced_length==1)
  42:                      {
  43:                          charArray[i] = replaceArray[0];
  44:                      }
  45:                  }
  46:              }
  47:   
  48:   
  49:              if (emptyCount == 0)
  50:              {
  51:                  return s;
  52:              }
  53:   
  54:              //no need to resize array and return the new string directly
  55:              if (replaced_length == 1)
  56:              {
  57:                  return new string(charArray);
  58:              }
  59:   
  60:              int newSeats = (replaced_length - 1) * emptyCount;
  61:              int oldSize = charArray.Length;
  62:              int newSize=oldSize+newSeats;
  63:              Array.Resize<char>(ref charArray, newSize);
  64:              for (int i = oldSize - 1; i >= 0; i--)
  65:              {
  66:                  if (charArray[i] == ' ')
  67:                  {
  68:                      for (int j = 0; j < replaced_length; j++)
  69:                      {
  70:                          charArray[newSize - j - 1] = to_be_replaced[replaced_length - j - 1];
  71:                      }
  72:                      newSize -=replaced_length ;
  73:                  }
  74:                  else
  75:                  {
  76:                      charArray[newSize - 1] = charArray[i];
  77:                      newSize--;
  78:                  }
  79:              }
  80:   
  81:              return new string(charArray);
  82:          }
  83:   
  84:          /// <summary>
  85:          /// use string builder
  86:          /// </summary>
  87:          public string ReplaceEmptyChar_2(string s, string to_be_replaced)
  88:          {
  89:              StringBuilder sb=new StringBuilder();
  90:              foreach(char c in s)
  91:              {
  92:                  if (c == ' ')
  93:                  {
  94:                      sb.Append(to_be_replaced);
  95:                  }
  96:                  else
  97:                  {
  98:                      sb.Append(c);
  99:                  }
 100:              }
 101:              return sb.ToString();
 102:          }
 103:   
 104:          /// <summary>
 105:          /// use string builder
 106:          /// </summary>
 107:          public string ReplaceEmptyChar_3(string s, string to_be_replaced)
 108:          {
 109:              StringBuilder sb = new StringBuilder(s.Length*2);
 110:              foreach (char c in s)
 111:              {
 112:                  if (c == ' ')
 113:                  {
 114:                      sb.Append(to_be_replaced);
 115:                  }
 116:                  else
 117:                  {
 118:                      sb.Append(c);
 119:                  }
 120:              }
 121:              return sb.ToString();
 122:          }
 123:   
 124:          /// <summary>
 125:          /// use list
 126:          /// </summary>
 127:          public string ReplaceEmptyChar_4(string s, string to_be_replaced)
 128:          {
 129:              List<char> list = new List<char>(s.Length * 2);
 130:              foreach (char c in s)
 131:              {
 132:                  if (c == ' ')
 133:                  {
 134:                      foreach (char c2 in to_be_replaced)
 135:                      {
 136:                          list.Add(c2);
 137:                      }
 138:                  }
 139:                  else
 140:                  {
 141:                      list.Add(c);
 142:                  }
 143:              }
 144:              return new string(list.ToArray());
 145:          }
 146:   
 147:          /// <summary>
 148:          /// unsafe pointer
 149:          /// </summary>
 150:          public unsafe string ReplaceEmptyChar_5(string s, char to_be_replaced)
 151:          {
 152:              int emptyCount = 0;
 153:              fixed (char* c = s)
 154:              {
 155:                  char* cp = c;
 156:                  for (int i = 0; i < s.Length; i++)
 157:                  {
 158:                      if (*cp == ' ')
 159:                      {
 160:                          *cp = to_be_replaced;
 161:                      }
 162:                      cp++;
 163:                  }
 164:   
 165:                  if (emptyCount == 0)
 166:                  {
 167:                      return s;
 168:                  }
 169:                  return new string(c);
 170:              }
 171:   
 172:          }
 173:   
 174:          public string ReplaceEmptyChar_6(string s, char to_be_replaced)
 175:          {
 176:              char[] charArray = s.ToCharArray();
 177:   
 178:              for (int i = 0; i < charArray.Length; i++)
 179:              {
 180:                  if (charArray[i] == ' ')
 181:                  {
 182:                      charArray[i] = to_be_replaced;
 183:                  }
 184:              }
 185:              return new string(charArray);
 186:          }
 187:   
 188:          /// <summary>
 189:          /// use string.Replace
 190:          /// </summary>
 191:          public string Replace_2(string s, string old, string to_be_replaced)
 192:          {
 193:              return s.Replace(old, to_be_replaced);
 194:          }
 195:   
 196:          /// <summary>
 197:          /// use string.Replace
 198:          /// </summary>
 199:          public string Replace_1(string s, char old, char to_be_replaced)
 200:          {
 201:              return s.Replace(old, to_be_replaced);
 202:          }
 203:   
 204:          /****
 205:           用指定字符串替换空白字符的三个实现比较,发现还是框架自带方法Replace快很多
 206:           Reflector一下发现Replace实现在COMString.cpp中,native的优势~~~~~
 207:           为了更深刻反映native的优势,通过ReplaceEmptyChar_5实现一个unsafe的方法,其中用到指针
 208:           该方法只简单做字符替换,而Replace也有一个字符替换的重载,同时实现一个managed版本的字符替换方法ReplaceEmptyChar_6
 209:           实现结果标明使用了unsfae的指针之后,性能也有较大提升
 210:           ****/
 211:      }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

replace empty char with new string,unsafe method和native implementation的性能比较的更多相关文章

  1. String.Join Method

    Overloads Join(String, String[], Int32, Int32) Concatenates the specified elements of a string array ...

  2. char数组和String互转

    char ch[100];string str; 把char*(c类型的string)数组转换为string:str = ch; //即可str.assign(ch); //也可 把string类型转 ...

  3. 对 static const char* const str[] = {“string”} 的理解

    static const char* const str[] = {“string”} 静态常量型指针变量 static const char* 常量型变量 const str[] 这样修饰的作用为 ...

  4. delphi char数组、string和Pchar的相互转换

    因为要调用windows的api或者给vc++写接口,很多地方都要用到pchar,现在将char数组.string和pchar之间的相互转换都列出来,都是网上找的资料,我总结一下,先直接上代码,再讲原 ...

  5. java字符数组char[]和字符串String之间的转换

    java字符数组char[]和字符串String之间的转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 使用String.valueOf()将字符数组转换成字符串 void (){ cha ...

  6. vector(char*)和vector(string)

    vector<char*> ch; vector<string> str; for(int i=0;i<5;i++) { char *c=fun1();//通过这个语句产 ...

  7. char数组与string转换

    1.char数组转string 有很多种方法: 假设c字符串定义为char ch[]="hello world!"; 1.向构造函数传入c字符串创建string对象: string ...

  8. 关于==和equals()方法&Java中string与char如何转换&String,StringBuffer

    1.对于基本数据类型,可以直接使用==和!=进行内容比较 如:int x=30;        int y=30;         x==y;  //true 基本数据类型 简单类型(基本类型) bo ...

  9. Java String Split Method

    Java String.split() method 有如下几种特殊情况: 1. 分隔符出现在首尾 public static void main(String args[]) { String St ...

随机推荐

  1. SGU 解题报告

    Volume 1 Volume 2

  2. Java Servlet 接收上传文件

    在Java中使用 Servlet 来接收用户上传的文件,需要用到两个apache包,分别是 commons-fileupload 和 commons-io 包: 如果直接在doPost中,使用requ ...

  3. web 中 bbs 例子(多次递归)

    数据库设计:create table `header`(  // 父表  parent int not null, //父级  poster varchar(20) not null, //作者  p ...

  4. windows 环境下安装plpython语言环境到postgresql数据库

    1.1       安装plpython 在windows环境 1.1.1      下载http://legacy.python.org/ftp//python/3.2.5/python-3.2.5 ...

  5. PHP - PHPExcel操作xls文件

    读取中文的xls.csv文件会有问题,网上找了下资料,发现PHPExcel类库好用,官网地址:http://phpexcel.codeplex.com/ 1.读取xls文件内容 <?php // ...

  6. postgresql info

    DROP TYPE IF EXISTS info CASCADE;CREATE TYPE info AS (state int4,operator varchar,time TIMESTAMP(0) ...

  7. iis post 请求.html文件报405

    其实本地文件默认是不允许post请求的,但是需要配置一下,配置如下: 我的iis版本是8.5             当然默认也是不能post请求  *.html或是*.json的的文件的,这个问题困 ...

  8. 【转】.Net程序员玩转Android系列之三~快速上手

    原文:http://www.cnblogs.com/HouZhiHouJueBlogs/p/3962122.html 快速环境搭建和Hello World 第一步:JAVA SDK(JDK)的安装: ...

  9. poj 1273

    网络流之最大流的基础题: 可以使用dinic算法和EK算法: 分别对着模板敲了一遍: dinic: #include<cstdio> #include<cstring> #in ...

  10. 纯手工全删除域内最后一个EXCHANGE--How to Manually Uninstall Last Exchange 2010 Server from Organization

    http://www.itbigbang.com/how-to-manually-uninstall-last-exchange-2010-server-from-organization/ 没办法, ...