字符串

Split分割字符串

static void Main(string[] args)
        {
            string s = "abc += def";
            //把不想要的+=放在字符串数组中
            char[] chs = {'+','='};
            //使用Split分割字符串,删除不想要的字符串,把空数据也移除掉
            string[] str = s.Split(chs,StringSplitOptions.RemoveEmptyEntries);
            Console.ReadKey();
        }

 

敏感词换成*号

 static void Main(string[] args)
        {
            string str = "我是大领导春哥";
            if (str.Contains("春哥"))
            {
               str =  str.Replace("春哥", "**");
            }
            Console.WriteLine(str);
            Console.ReadKey();
        }

 

Substring 截取字符串

 static void Main(string[] args)
        {
           //Substring 截取字符串
           string str = "今天天气好晴朗,处处好风光";
           str = str.Substring(0,2);//只截取“今天”
           str = str.Substring(2);//第三个字开始的数据全要
           Console.WriteLine(str);
        }

 

StartsWith 判断字符串是不是以指定的前缀开始

static void Main(string[] args)
        {
            //StartsWith 判断字符串是不是以指定的前缀开始
            string str = "今天天气好晴朗,处处好风光";
            if (str.StartsWith("今天"))
            {
                Console.WriteLine("是的");
            }
            else
            {
                Console.WriteLine("不是的");
            }
            Console.ReadKey();
        }

 

EndsWith 判断字符串是否以指定后缀结尾
static void Main(string[] args)
        {
            //EndsWith 判断字符串是否以指定后缀结尾
            string str = "今天天气好晴朗,处处好风光";
            if (str.EndsWith("风光"))
            {
                Console.WriteLine("是的");
            }
            else
            {
                Console.WriteLine("不是的");
            }
            Console.ReadKey();
        }

 Indexof:该字符第一次出现的位置

static void Main(string[] args)
        {
            string str = "今天天气好晴朗,处处好风光";
            int index = str.IndexOf("");//晴字第一次出现的位置
            Console.WriteLine(index);
            Console.ReadKey();
        }

LastIndexOF:该字符最后一次出现的位置

static void Main(string[] args)
        {
            string str = "今天天气好晴朗,处处好风光";
            int index = str.LastIndexOf("");//该字符最后一次出现的位置
            Console.WriteLine(index);
            Console.ReadKey();
        }

 Join:将指定字符分别放到每个元素的后面

 static void Main(string[] args)
        {
            string[]  names = { "张三", "李四", "王五","赵六","田七"};
            string strNew = string.Join("|",names);
            Console.WriteLine(strNew);
            Console.ReadKey();
        }

 

posted @ 2022-12-25 20:23  小春博客  阅读(30)  评论(0编辑  收藏  举报