AI智能
改变未来

C#学习.字符串的使用1

1.获取字符串的长度Length

        static void Main(string[] args)
        {
            Console.WriteLine(\”请输入你所想的名字:\”);
            string name = Console.ReadLine();
            Console.WriteLine(name.Length);
            Console.ReadLine();
        }

 

2.将字符串转化为大写字母ToUpper

        static void Main(string[] args)
        {
            Console.WriteLine(\”请输入同学A喜欢的课程:\”);
            string lessonone = Console.ReadLine();
            //将字符串转化为大写
            lessonone = lessonone.ToUpper();
            Console.WriteLine(\”请输入同学B喜欢的课程:\”);
            string lessontwo = Console.ReadLine();
            lessontwo = lessontwo.ToUpper();
            if (lessonone == lessontwo)
            {
                Console.WriteLine(\”两个同学喜欢的课程一样\”);
            }
            else
            {
                Console.WriteLine(\”两个同学喜欢的课程不一样\”);
            }
            Console.ReadLine();
        }

 

3.将字符串转换为小写字母ToLower

    static void Main(string[] args)
        {
            Console.WriteLine(\”请输入同学A喜欢的课程:\”);
            string lessonone = Console.ReadLine();
            //将字符串转化为小写
            lessonone = lessonone.ToLower();
            Console.WriteLine(\”请输入同学B喜欢的课程:\”);
            string lessontwo = Console.ReadLine();
            lessontwo = lessontwo.ToLower();
            if (lessonone == lessontwo)
            {
                Console.WriteLine(\”两个同学喜欢的课程一样\”);
            }
            else
            {
                Console.WriteLine(\”两个同学喜欢的课程不一样\”);
            }
            Console.ReadLine();
        }

4.比较两个字符串是否相等Equals

        static void Main(string[] args)
        {
            Console.WriteLine(\”请输入同学A喜欢的课程:\”);
            string lessonone = Console.ReadLine();
            Console.WriteLine(\”请输入同学B喜欢的课程:\”);
            string lessontwo = Console.ReadLine();
            if (lessonone.Equals(lessontwo))
            {
                Console.WriteLine(\”两个同学喜欢的课程一样\”);
            }
            else
            {
                Console.WriteLine(\”两个同学喜欢的课程不一样\”);
            }
            Console.ReadLine();
        }

5.分割字符串Split

       static void Main(string[] args)
        {
            string s = \”a  b  d  e ,, ffff\”;
            char[] ch = {\’ \’, \’,\’, \’_\’ };
            string[] str = s.Split(ch,StringSplitOptions.RemoveEmptyEntries);//去掉分割后的\’\’字符
            for (int i = 0; i < str.Length; i++)
            {
                Console.WriteLine(str[i]);
            }
            Console.ReadLine();
        }
    }

        static void Main(string[] args)
        {
            //将2008-08-08转换成2008年08月08日
            string s = \”2008-08-08\”;
            char[] chr = { \’-\’ };
            string[] str = s.Split(chr, StringSplitOptions.RemoveEmptyEntries);
            Console.WriteLine(\”{0}年{1}月{2}日\”, str[0], str[1], str[2]);
            Console.ReadLine();
        }

 

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » C#学习.字符串的使用1