C#使用String和StringBuilder运行速度测试及各自常用方法简介

2020-01-05 09:50:58于丽

C#,String,StringBuilder,运行速度


using System;
using System.Diagnostics;
using System.Text;
namespace StringBuild时间测试
{
 class Program
 {
  static void Main(string[] args)
  {
   Stopwatch sw = new Stopwatch();
   sw.Start();
   string num = "";
   for (int i = 0; i < 50000; i++)
   {
    num += i;
   } 
   sw.Stop();
   Console.WriteLine(sw.Elapsed);
   Console.ReadKey();
  }
 }
}

使用String运行结果:

C#,String,StringBuilder,运行速度

常用方法简介:

String

1,CompareTo()方法,比较字符串的内容 (根据assice比较大小,相等返回0,大的返回1,小则返回-1) 

2,Replace()用另一个字符(字符串)替换字符串中给定的字符(字符串) 

3,Split()在出现给定字符的地方,把字符串拆分称一个字符串数组(根据(参数)分解字符串,形成新的字符串) 

4,SubString()在字符串中检索给定位置的子字符串(截取字符串返回子串,两个重载) 

5,ToLower()把字符串转换成小写形式