C#——字符串

不可变性

在讲解字符串的时候,主要强调了字符串的一个特殊的性质:不可变性。

当我们给一个字符串赋值后,如:str="Rebecca":然后我们再给它赋予一个新值:str="小扁扁"。这里Rebecca并没有消失,而是新值“小扁扁”重新有了一块控件存储的新值。只是str的指向变了,一开始指向的是Rebecca,后来有了新值,指向“小扁扁”

字符串特点

  • 字符数组变成字符串——ToString()

  • 字符串变成小写、大写——ToLower()、ToUpper ()
            Console.WriteLine("输入第一门课程");
            string str1 = Console.ReadLine();
            Console.WriteLine("输入第二门课程");
            str1 = str1.ToLower();//把字符串转换成小写的
            string str2 = Console.ReadLine();
            str2 = str2.ToLower();//把字符串都转换成小写的
            if (str1 ==str2)
            {
                Console.WriteLine("课程一样" + str1);
            }
            else
            {
                Console.WriteLine("课程不一样{0}---{1}", str1, str2);
            }
            
            Console.ReadKey();

  • 忽略大小写比较——Equals()
            Console.WriteLine("输入第一门课程");
            string str1 = Console.ReadLine();
            Console.WriteLine("输入第二门课程");
            string str2 = Console.ReadLine();

            bool result = str1.Equals(str2, StringComparison.OrdinalIgnoreCase);//忽略大小写

            if (result )
            {
                Console.WriteLine("课程一样" + str1);
            }
            else
            {
                Console.WriteLine("课程不一样{0}---{1}", str1, str2);
            }
            
            Console.ReadKey();

  • 切割字符串中不想要的字符——Split()
        string str = "你--- 好,美 ---玲    儿";
        char[] chs = new char[] {' ','-' };//消除字符串中不想要的字符空格和-
        string[] result = str.Split(chs, StringSplitOptions.RemoveEmptyEntries);//消除chs的内容
        for (int i = 0; i < result.Length; i++)
        {
           Console.Write(result[i]);//去初之后的内容留下来
        }              
         Console.ReadKey();

输入的结果为:你好,美玲儿

  • 替换、移除字符串中的字符——Replace()、Remove()
        string st ="abcdef";
        string newstring=st.Replace('a','x');
        Console.WriteLine(newstring);//即变为了xbcdef
        string st ="abcdef";
        string newstring=st.Remove(2);//去除第2位之后的所有字符
        Console.WriteLine(newstring);//即变为了ab

  • 移除字符串中指定的字符串——Trim(清空格)      
        string st="abc  d  e  f ";
        string newstring =st.Trim( );//找到字符串abcef中的空格移除
        Console.WriteLine(newstring);//即abcdef
           string st="abcdef ";
            string newstring =st.Trim(new char[]{'a'} );//找到字符串abcef中的a移除
            Console.WriteLine(newstring);//即bcdef

  • 提取字符串中指定的字符串——Substring()
          string st="abcdef ";
          string newstring =st.Substring(2);//找到字符串abcef中的第2个,提取第2个之后的
          Console.WriteLine(newstring);//即cdef
           string st="abcdef ";
            string newstring =st.Substing(2,2);//找到字符串abcef中的第2个后面的两个提取出来
            Console.WriteLine(newstring);//即cd

  • 通过获取字符串的开始索引值——IndexOf()

如果字符串中找到了开始的索引值那么返回索引值,如果没有那么返回-1

       string st="abcdef ";
        string newstring =st.IndexOF("bcd" );//找到字符串abcef中的bcd移除
        Console.WriteLine(newstring);//即1

  • 判断字符串中是否有字符串A——Contains
       string st="ab cd ef ";
        bool newstring =st.Contains("cd" );//找到字符串abcef中的cd
        Console.WriteLine(newstring);//即true

  • 拆分——Split



猜你喜欢

转载自blog.csdn.net/xml1996/article/details/81053272