using System;
using System.Diagnostics;
using System.Text;
namespace _05StringBuilder
{
class Program
{
static void Main(string[] args)
{
//int[] a = null;
StringBuilder sb = new StringBuilder();
string str = null;
string str_1 = null;
Stopwatch sw = new Stopwatch();
sw.Start();
for(int i = 0; i<100000;i++)
{
//str += i;
sb.Append(i);
}
sw.Stop();
//str.Equals(str_1, StringComparison.OrdinalIgnoreCase);
//Console.WriteLine(sb.ToString());
//Console.WriteLine(sw.Elapsed); //00:00:06.3829584 VS 00:00:00.0031713
//Console.ReadKey();
string s_1 = "a b dfd_ + = ,,,fdf ";
char[] chs_1 = { ' ', '_', '+','=',',' };
string[] str_2 = s_1.Split(chs_1, StringSplitOptions.RemoveEmptyEntries); //filter掉chs_1中的,
//返回一个字符串数组
for (int i = 0; i < str_2.Length; i++)
{
Console.WriteLine(str_2[i]);
}
string str_3 = "国家关键人物老赵";
if (str_3.Contains("老赵"))
{
str_3 = str_3.Replace("老赵", "**");
}
//Console.WriteLine(str_3);
string str_4 = "锄禾日当午,汗滴禾下土";
str_4 = str_4.Substring(1,2); //截取开始的索引,长度
string path = @"c:\a\c\b\d\\fd\\dsd\sd\s\d\哈哈!"; //屏蔽转义符
int index_1 = path.LastIndexOf("\\"); //两个\\是一个\
path = path.Substring(index_1 + 1);
Console.WriteLine(path);
}
}
}
字符串操作
最新推荐文章于 2026-01-09 14:40:17 发布
本文展示了C#中使用StringBuilder进行字符串拼接的性能优势,以及字符串的分割、替换和查找操作。通过Stopwatch计时展示了StringBuilder在大量拼接时比字符串连接更快。此外,还演示了如何过滤字符串中的特定字符并获取子字符串,以及处理路径中的目录层级。
2665

被折叠的 条评论
为什么被折叠?



