字符串中的智慧(一)

字符串可大可小,就像我们一样(*^__^*) 

一、字符串和数组

字符串变成char数组

字符数组变成字符串

string text = "你好,好久不见";
char[] chs = text.ToCharArray(); //把字符串text转换成char数组
text=new string(chs);//把char数组转换成string(上面text已经定义成string)
Console.WriteLine(chs);
console.Readline();</span>

二、内部改动

1、Startswith开始

     Endswith最后

2、Contains是否包含,

   Replace(旧,新)替换。

3、Split(‘*’)从整体中移除“*”,

   Remove(*,%)从“*”开始移除% 个,

Substring(*,&)从*开始截取&个字符,&可不写默认截取到最后。

            string str = "你好CSDN+++++++++你好people";
            Console.WriteLine(str );

            Console.WriteLine("++++++++++++++++++++++++++++++++++++++++");
            str = str.Remove(6, 9);// 从第6字符开始移除9个字符
            Console.WriteLine(str);

            Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++");
            str = str.Replace("CSDN", ",善良;");//替换“CSDN”为“,善良,”
            Console.WriteLine(str);
           
            Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++");
            str = str.Substring(6);//截取字符串,从此往前都舍弃
            Console.WriteLine(str );

            //是否以某个字符串开始
            Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            bool result = str.StartsWith("你");

            if (result )
            {
                Console.WriteLine("从“你”开始");
            }
            else
            {
                Console.WriteLine("不是从“你”开始");
            }
           
            //bool result=str.startswith(e)I;是否以“e”结束。</span>

            Console.WriteLine("++++++++++++++++++++++++++++++++++++");
            bool resul = str.Contains ("纯洁");//是否包含“纯洁”?

            if (resul )
            {
                Console.WriteLine("包含“纯洁”字符串");
            }
            else 
            {
                Console.WriteLine("没有”纯洁”内容");
            }
           
                Console.ReadKey();  //从email总提取用户名和域名:
             string email = "[email protected]";
             string[] st= email.Split('@');//去除
             Console.WriteLine("用户名{0}",st[0]);
             Console.WriteLine("域名是{0}",st[1]);

             //包含  替换  去除
             string date = "2015_____11______02";
             char[] chs = new char[] { '_' };  //去除“_”
             string[] time = date.Split(chs, StringSplitOptions.RemoveEmptyEntries);        
             Console.WriteLine("{0}year{1}month{2}day", time[0], time[1], time[2])                    
            Console.ReadKey();


字符串中的智慧(一)_第1张图片

还有一个关于索引和大小写转换、判断的,下篇博客见咯……

你可能感兴趣的:(字符串,C#)