C#视频学习——字符串

01字符串的三个特性

①string可以看做是char的只读数组。

②C#中字符串有一个重要特性:不可变性,字符串一旦声明就不再可以改变。

③如果要对char进行修改,那么就必须创建一个新的字符串,用ToCharArray()方法得到字符串的char数组,对数组进行修改后,调用new string 这个构造函数来创建char数组的字符串。



02字符串的应用

            ////练习一:随机输入你心中想得到的一个名字,然后输出它的字符串长度 

            Console.WriteLine("输入你心目中想到的名字");

            string name = Console.ReadLine();

            Console.WriteLine(name.Length);

            Console.ReadKey();


            //练习二:两个学员输入各自最喜欢的课程名称,请判断是否一致,如果相等,则输出你们俩喜欢相同的课程。
            //如果不相同,则输出你们俩喜欢不相同的课程

            Console.WriteLine("输入第一门课程");
            string str1 = Console.ReadLine();
            str1 = str1.ToLower();  //把字符串转换成小写的

            Console.WriteLine("请输入第二门课程");           
            string str2 = Console.ReadLine();
            str2 = str2.ToLower();

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



03字符串大小写转换

str1=str1.ToUpper();   //把字符串转换成大写

str1=str1.ToLower();   //把字符串转换成小写



04忽略大小写比较的方法

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



05字符串元素的移除与拼接




06一切类型都可以调用.ToString()方法转换成字符串类型。例如:

int num = 10
string s = num.ToString();
Console.WriteLine();
Console.ReadKey();



07字符串的替换

            string name = "老佩";

            name=name.Replace('佩','莱');

            Console.WriteLine(name);
            Console.ReadKey();


08判断字符串中是否包含子字符串

        static void Main(string[] args)
        {
            string name = "老苏不纯洁";

            bool result = name.Contains("纯洁");      //判断这个字符串中是否包含子字符串
            if (result)
            {
                name = name.Replace("纯洁", "洁纯");        //替换字符串
                Console.WriteLine(name);
            }
            else
            {
                Console.WriteLine(name);
            }

            Console.ReadKey();
                
        }



09截取字符串

        static void Main(string[] args)
        {
            string name = "老苏不纯洁";
            name = name.Substring(3);     //截取字符串
            Console.WriteLine(name);
            Console.ReadKey();
                
        }



10判断字符串是否以某个字符串开始

        static void Main(string[] args)
        {
            string str = "小杨很纯洁";
            bool result = str.StartsWith("小杨");        //判断字符串是否以某个字符串开始

            if (result)
            {
                Console.WriteLine("有这个字符串");
            }
            else
            {
                Console.WriteLine("没有这个字符串");
            }
            Console.ReadKey();
                
        }



11判断字符串是否以某个字符串结束的

        static void Main(string[] args)
        {
            string str = "小杨很纯洁";
            bool result = str.EndsWith("纯洁");       //判断字符串是否以某个字符串结束的

            if (result)
            {
                Console.WriteLine("有这个字符串");
            }
            else
            {
                Console.WriteLine("没有这个字符串");
            }
            Console.ReadKey();
                
        }



12找字符串的索引

int index = str.IndexOf("");        //找括号内字符串的索引
int index = string.LastIndexOf("最后的字符串");        //找最后一个字符串的索引  



13 StringBulider和String的区别

String在进行运算时会产生一个新的实例,而StringBuilder则不会。所以在大量字符串拼接或频繁对某一字符串进行操作时最好使用StringBuilder,不要使用String



14在某个索引处插入一个字符串

st=st.Insert(7,"!");



15字符串练习题(1-3)






16Join方法的应用




猜你喜欢

转载自blog.csdn.net/delicious_life/article/details/80241347
今日推荐