第十节 常用字符串的处理方法

View Code
案例1:LastIndexOf

 class Program
    {
        static void Main(string[] args)
        {
            //LastIndexOf
            string str = "Welcome!";
            int index = str.LastIndexOf('o');
            Console.WriteLine("'o'在字符串{0}里的索引为:{1}", str, index);
            index = str.LastIndexOf('t');
            if (index == -1)
            {
                index = str.LastIndexOf('o');
                Console.Write("'t'在字符串{0}不存在", str);

            }
            else
            {
                Console.Write("'t'在字符串{0}的索引为:{1}", str,index);
            }

            Console.ReadKey();

        }
    }

 

———————————————————————————————————————————————————————

案例2:SubString 截取

class Program
    {
        static void Main(string[] args)
        {
            
            string str = "Welcome to NIIT!!!";
            //SubString方法用于对字符串进行截取操作,返回一个截取后的子串
            string substr = str.Substring(11);

            Console.WriteLine("str:" + str);

            Console.WriteLine("substr:" + substr);


            //还可以为SubString制定截取的字符的个数
            substr = str.Substring(11, 4);
            Console.WriteLine("substr:" + substr);

 

            Console.ReadKey();

        }
    }

 

————————————————————————————————————————————————————————————

案例3:转化大小写 Tolower()

string str = "WELCOOM TO NITT";
            //ToLower()用于将字符转换为小写字母并返回转换后的字符串
            string lowStr = str.ToLower();

            Console.WriteLine("转换为小写"+lowStr);
            //ToLower()用于将字符转换为大写字母并返回转换后的字符串
            string upperStr=lowStr.ToUpper();
            Console.WriteLine("转换为大写:"+upperStr);
            Console.ReadKey();

 

——————————————————————————————————————————————————————————

案例4 使用Trim()效果

string str = "               welcome to NIIT            ";
            Console.WriteLine("使用Trim()前的效果:{0}",str);
            //用于除去字符串前后空白,并返回去除空白后的字符串
            string trimStr = str.Trim();
            Console.WriteLine("使用Trim()前的效果:{0}",trimStr);
            Console.ReadKey();

————————————————————————————————————————————————————————————

案例5    IsNullOrEmpty方法用于判断字符串是否是一个空引用(null)或者空串
 //IsNullOrEmpty方法用于判断字符串是否是一个空引用(null)或者空串
            string str = "";
            String str1 = String.Empty;
            Console.WriteLine("该字符串是否为空串:"+string.IsNullOrEmpty(str));
            Console.WriteLine("该字符串是否为空串2:" + String.IsNullOrEmpty(str1));

            string str2 = null;

            Console.WriteLine("该字符串是否为空引用:" + String.IsNullOrEmpty(str2));

            Console.ReadKey();

————————————————————————————————————————————————————————————

案例6:  实践例子  获取邮箱用户名·

 string email;
            string chose;
            do
            {
                Console.Write("请输入您的邮箱地址:");
                email = Console.ReadLine();
                Console.WriteLine("你输入的Email地址是:{0}", email);
                int position=email.IndexOf();
                Console.WriteLine(position);
                if (!string.IsNullOrEmpty(email) && position != -1)
                {
                    string username = email.Substring(0, position);
                    Console.WriteLine("你的用户名是:{0}", username);

                }
                else
                {
                    Console.WriteLine("你输入的邮箱格式不正确!");
                }

                Console.Write("是否继续?(yes/no)");
                chose = Console.ReadLine();
                chose = chose.Trim().ToLower();
                Console.WriteLine(email);

            }
            while (chose.Equals("yes"));


  Console.ReadKey();

————————————————————————————————————————————————

案例7 :分割字符  连接字符

Console.Write("请输入一个字符串,用空格分隔");
            string str = Console.ReadLine();

            //分割字符串
            string[] a = str.Split(' ');
            Console.WriteLine("分隔后:");

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

            String joinstr = String.Join("_", a);
            Console.WriteLine(joinstr);

             Console.ReadKey()

——————————————————————————————————————————

案例8:Format  制表!


            Console.Write("请输入姓名:");
            string name = Console.ReadLine();
            Console.Write("请输入身高:");
            string height = Console.ReadLine();
            Console.Write("请输入血型:");
            string bloodType = Console.ReadLine();
            Console.Write("请输入星座:");
            string planet = Console.ReadLine();
            Console.Write("出生年月:");
            string birthday = Console.ReadLine();
            Console.Write("喜欢吃的食物:");
            string favourFood = Console.ReadLine();
            Console.WriteLine();
            Console.WriteLine("个人信息:");

           string b=string.Format("name:{0}\nheight:{1}\nbloodType:{2}\nplanet:{3}\nbirthday:{4}\nfavourFood:{5}",
                name, height, bloodType, planet, birthday, favourFood);
           Console.WriteLine(b);
           Console.ReadKey();

 

posted @ 2012-06-20 10:50  ComBat  阅读(118)  评论(0编辑  收藏  举报