C#字符串的处理(原创)--王超C#

字符串string可以看成char类型的只读数组,不能赋值,只能读值。

string a = "abcdefg";
            for (int i = 0; i < a.Length; i++)
            {
                Console.WriteLine(a[i]);
            
            }
            Console.ReadKey();

可以用ToCharArray()把只读数组改为可写数组;

string a = "abcdefg";
           
            for (int i = 0; i < a.Length; i++)
            {
                Console.WriteLine(a[i]);
            
            }

            char[] b = a.ToCharArray();

            for (int i = 0; i < b.Length; i++)
            {
                Console.WriteLine(b[i]);

            }

            for (int i = 0; i < b.Length; i++)
            {
                b[i] = 'x';

            }
            for (int i = 0; i < b.Length; i++)
            {
                Console.WriteLine(b[i]);

            }
            Console.ReadKey();

可以把一个字符串string变成大写ToUpper()和变成小写ToLower();注意可以使用自身改变;

string a = "abc";
            a = a.ToUpper();

            Console.WriteLine(a);

            a = a.ToLower();

            Console.WriteLine(a);

            Console.ReadKey();

去掉字符串的空格Trim();去掉左边空格TrimStart()和去掉右边空格TrimEnd();

string a = "    abc     ";
            string b = "    abc     ";
            string c = "    abc     ";


            Console.WriteLine(a);

            a = a.Trim();

            Console.WriteLine(a);

            b = b.TrimStart();

            Console.WriteLine(b);

            c = c.TrimEnd();

            Console.WriteLine(c);

            Console.ReadKey();

添加空格PadLeft()和PadRight()

string a = "abc";

            a = a.PadLeft(10);

            Console.WriteLine(a);

            a = a.PadRight(20);

            Console.WriteLine(a);

            a = a.PadLeft(30, '-');

            Console.WriteLine(a);
            Console.ReadKey();

 

posted on 2013-01-18 10:40  王超  阅读(248)  评论(0)    收藏  举报