字符串操作

本文展示了C#中使用StringBuilder进行字符串拼接的性能优势,以及字符串的分割、替换和查找操作。通过Stopwatch计时展示了StringBuilder在大量拼接时比字符串连接更快。此外,还演示了如何过滤字符串中的特定字符并获取子字符串,以及处理路径中的目录层级。
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);
        }
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值