string类中常用方法总结

1、将一个字符串转换成字符数组
            char[] words = text.ToCharArray(2,3); //括号内2,3表示从指定下标为2的字符开始,3表示转换3个字符
            char[] words1 = text.ToCharArray();//表示将text字符串转换成字符数组
2、将一个字符数组转换成字符串
            text = new string(words1);
            Console.WriteLine(text);
            Console.WriteLine("^^^^^^^^^^^^^^^^^^^^^^~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^");
3、将一个字符串中的英文转换成小写
            string text2 = "My Name is Aobama";
            text2 = text2.ToLower();
            Console.WriteLine(text2);
            Console.WriteLine("^^^^^^^^^^^^^^^^^^^^^^^~~~~~~~~~~~~~~~~~~~~~~~~^^^^^");
4、将一个字符串中的英文转换成大写
            text2 = text2.ToUpper();
            Console.WriteLine(text2);
            Console.WriteLine("^^^^^^^^^^^^^^^^^^^^^^^^~~~~~~~~~~~~~~~~~~~~~~~^^^^");
5、比较两个字符串,不考虑其大小写情况,返回一个bool值
            string text3 = "my name is AoBaMa";
            bool result = text3.Equals(text2, StringComparison.OrdinalIgnoreCase);
            if (result)
            {
                Console.WriteLine("不考虑大小写,他们是一样的!");
            }
            else
            {
                Console.WriteLine("他们完全不同");
            }
            Console.WriteLine("^^^^^^^^^^^^^^^^^^^^^^^^^^^^~~~~~~~~~~~~~~~~~");
6、查看字符串中是否含有某个字词,返回一个bool值
            string txt = "你是usb!";
            bool result1 = txt.Contains("usb");
            if (result1)
            {
                txt = txt.Replace("usb", "文明用语");   //7、将字符串中的某个字词替换掉
                Console.WriteLine(txt);
            }
            else
            {
                Console.WriteLine(txt);
            }
            Console.WriteLine("^^^^^^^^^^^^^^^^^^^^^^^^^^^^~~~~~~~~~~~~~~~~~~~");
8、将字符串按特定的char[] 数组进行分割 返回一个字符串数组
            string say = "我的  名字 是-神-,今年 22岁了";
            char[] temp = new char[] { ' ', '-' };
            string[] t = say.Split(temp);     //此方法切割后可能有空字符串
            string[] y = say.Split(temp, StringSplitOptions.RemoveEmptyEntries);//9、此方法为切割后返回不包含有空字符串的字符串数组
            for (int i = 0; i < y.Length; i++)
            {
                Console.Write(y[i]);
            }
            Console.WriteLine();
            Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~……………………~~~");

10、此方法是吧字符串数组的元素按某个特定的分隔符连接起来并返回一个新字符串,此方法为静态的
            string x = string.Join("|", t);   
            Console.WriteLine(x);
            Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~……………………~~~");
11、从指定的下边开始从字符串中取字符串
            string tab = "你好,欢迎来到天朝,在天朝你将倍感幸福!";
            string tab1 = tab.Substring(5);  //从下标为5的字符开始到最后个字符
            string tab2 = tab.Substring(5, 4);  //5表示从下边为5的开始,4表示取的长度即只取四个字符
            Console.WriteLine(tab2);
            Console.WriteLine(tab1);
            Console.WriteLine("***********************************************");
12、判断字符串是否以某个字符开头的,返回一个bool值
            tab = "你好,欢迎来到天朝,在天朝你将倍感幸福!";
            bool r= tab.StartsWith("好");
            if (r)
            {
                Console.WriteLine("是这个开头的");
            }
            else
            {
                Console.WriteLine("不是这个开头的");
            }
            Console.WriteLine("_________________________________________________________________");

13、判断字符串是否以某个字符结尾的,返回一个boo了值
            tab = "你好,欢迎来到天朝,在天朝你将倍感幸福!";
            if (tab.EndsWith("幸福!"))
            {
                Console.WriteLine("是这个结尾的");
            }
            else
            {
                Console.WriteLine("不是这个结尾的");
            }
            Console.WriteLine("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
14、此方法用于找某个字符在字符串中的索引位置,返回下标值,如果没有此字符则返回-1
            tab = "你好!,欢迎来!到!天朝!";
            int num = tab.LastIndexOf("!",9);  //此方法用于找最后个符合条件的,9表示找到下标为9的字符时就停止
            Console.WriteLine(num);
            int num1 = tab.IndexOf("!", 7);//此方法用于找第一个符合条件的,7表示从下标为7的字符开始搜索
            Console.WriteLine(num1);
            Console.WriteLine("¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥");

15、此方法用于在指定的下标处添加个字符串并返回添加后的字符串
            string ab = "我是个学生";
            string abc = ab.Insert(3, "好好");
            Console.WriteLine(abc);
            Console.WriteLine("****************************************************");
16、  此方法用于判断字符串是否null或者“”空字符串,是返回true否则返回false
            string abd = "";
            bool isf=string.IsNullOrEmpty(abd);
            if (isf)
            {
                Console.WriteLine(isf);
            }
            else
            {
                Console.WriteLine(isf);
            }
            Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
17、此方法用于删除从指定字符开始的字符后返回一个字符串
            string abcd = "你是个好人,很好的人";
            abcd = abcd.Remove(3,5);   //此处3表示从下标为3的字符开始删起,5表示删5个字符,没有指定个数则全删了
            Console.WriteLine(abcd);
            Console.WriteLine("____________________________________________--");
18、此方法用于将用户输入的内容前面和后面的空格去掉
            Console.WriteLine("请输入用户名:");
            string useName =Console.ReadLine();
            useName = useName.Trim();
            Console.WriteLine(useName);

posted @ 2012-09-21 22:33  Wedn.Net  阅读(179)  评论(0编辑  收藏  举报