C#中关于字符串的常用知识点

namespace _02字符串的特性
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 冗余代码1)
            //string name = "字符串实际上相当于字符数组";
            //char[] chs = name.ToCharArray();//字符串变成字符数组;
            //for (int i = 0; i < chs.Length; i++)
            //{
            //    chs[i] = '哈';
            //}
            //Console.WriteLine(new string(chs));//把数组转变成字符串,并输出
            ////// name[2] ='女'; //属性改变了字段;
            //// char ch = name[2];
            //// name = "小笼包";
            //// Console.WriteLine(ch);
            //// //Console.WriteLine(name[2]);
            //Console.ReadKey();//读,获取
            #endregion


            #region 冗余代码2)
            //string name = "今天天气很好";
            ////string name = null;//------垃圾回收器,可以回收了

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


            //3)
            //if (string.IsNullOrEmpty(name))
            //{
            //    Console.WriteLine("为空");
            //}
            //else
            //{
            //    Console.WriteLine("有值");
            //}
            //Console.ReadKey();

            #endregion


            //4)转小写
            //string msg = "How Are You";
            //msg=msg.ToLower();//此时msg在内存中开了两个空间
            //Console.WriteLine(msg);
            //Console.ReadKey();

            //5)转大写
            //string msg = "How Are You";
            //msg = msg.ToUpper();//此时msg在内存中开了两个空间
            //Console.WriteLine(msg);
            //Console.ReadKey();


            //6)比较两个字符串是否相同(实际上判断对象是否是同一个对象)
            //string msg1 = "哈哈";
            //    string msg2 = "呵呵";
            //if (string.Equals(msg1, msg2))
            //{
            //    Console.WriteLine("相同");
            //}
            //else
            //{
            //    Console.WriteLine("不相同");

            //}
            //Console.ReadKey();


            //7)判断对象是否是同一个对象

            //Person p1 = new Person();
            //p1.Name = "小黑";
            //Person p2 = new Person();
            //p2.Name = "小黑";

            //if (object.ReferenceEquals(p1, p2))
            //{
            //    Console.WriteLine("相同");
            //}
            //else
            //{
            //    Console.WriteLine("不相同");
            //}
            //Console.ReadKey();//因为不是一个对象,最终输出"不相同”;


            //string msg = "Someone will like you";
            ////8)查找一个字符或者字符串,找到了返回的是该字符串的索引,找不到返回的是-1;

            //int index = msg.IndexOf("w");
            //Console.WriteLine(index);
            //Console.ReadKey();//输出为8

            //9)查找字符串最后一次出现的索引
            //string msg = "飞流直下三千尺三";

            //int index = msg.LastIndexOf("三");
            //Console.WriteLine(index);
            //Console.ReadKey();


            //截取字符串
            //string msg = "小黑嘿嘿嘿真黑";

            //msg = msg.Substring(3);//从索引为3的位置开始截取,一直到最后
            //Console.WriteLine(msg);
            //Console.ReadKey();//输出”嘿嘿真黑“;

            ////从索引为3的位置开始截取,一个长度为3的字符串
            //msg = msg.Substring(2, 3);
            //Console.WriteLine(msg);
            //Console.ReadKey();

            //分割 切割   以及移除空白项
            //string name = "卡卡西,messi,,,乔布斯,,,,,乔峰";
            //string[] msgs = name.Split(new char[] { ',' },StringSplitOptions.RemoveEmptyEntries);//Split  对单个字符进行分割
            //for (int i = 0; i < msgs.Length; i++)
            //{
            //    Console.WriteLine(msgs[i]);//可用运行到光标处,查看有多少项
            //}
            //Console.ReadKey();

            ///字符串连接
           // string[] names = { "王思聪", "马云", "messi" };
            
           //string txt = string.Join("☆",names);
           // Console.WriteLine(txt);
           // Console.ReadKey();




        }



        class Person
        {
            public string Name { get; set; }
        }
        
    }

}

猜你喜欢

转载自blog.csdn.net/nsjlive/article/details/81098340