C# 字符串处理小节

//两种字符串比较       

      /* string str1 = "apple";             

          string str2 = "apple";

             long t1 = System.DateTime.Now.Ticks;

             for (long i = 0; i < 10000000; i++)

             { if (str1 == str2) { } }

             long t2 = System.DateTime.Now.Ticks;

             for (long i = 0; i < 10000000; i++)

             { if (str1.Equals(str2)) { } }

             long t3 = System.DateTime.Now.Ticks;

             Console.WriteLine("str1==str2方法用时:" + (t2 - t1) + "str1.Equals(str2)方法的用时:" + (t3 - t2));

             Console.ReadKey();*/

            //C#字符串模糊比较问题:

            /* string num1 = "123456";

             string num2 = "123432;123456;334532;33565464";

             if (num2.Contains(num1))

             {

                 Console.WriteLine("num2 包含num1");

             }

             else

             {

                 Console.WriteLine("false");

             }

             Console.ReadKey();*/

 // indexOf()方法进行判断
            /* string num1 = "123456";
             string num2 = "123432;123456;334532;33565464";
             if (num2.IndexOf(num1) >-1)
             {
                 Console.WriteLine("num2 包含num1");
             }
             Console.ReadKey();*/

//字符串定位问题//1
            /*string str1 = "窗内网#http//www.itzcn.com*";
            int a= str1.IndexOf('*');
            int b = str1.IndexOf('#');
            string getstr = string.Empty;
            int[] c = new int[] {a,b };
            Array.Sort(c);

            foreach (int i in c)
            {
                if (a==i)
                {
                    getstr = "*";
                    break;
                }
                else
                {
                    getstr = "#";
                    break;
                }
            }
            Console.WriteLine(getstr);
            Console.ReadKey();*/
            //2
            /*string str1 = "大家好";
            string str2 = "好";
            if(str1.EndsWith(str2))
            {
                Console.WriteLine("字符串str1末尾与字符串str2相等");
            }
            Console.ReadKey();*/

//检索字符串中的某个字符出现的次数
            /* string str = "hello,please come to my office the afernoon";
             string s = "o";
             int start = 0;
             int count = 0;
             while (start != -1)
             {
                 start = str.IndexOf(s,start);
                 if (start != -1)
                 {
                     count++;
                     start++;
                 }
             }
             Console.WriteLine(count.ToString());
             Console.WriteLine(start.ToString());
             Console.WriteLine(str.Length);
             Console.ReadKey();*/

 //去掉字符串中指定的子字符串的问题
           /* string s = "<img a></br>123456<img a></br><img a></br>asdddss";
            s = s.Replace("</br>","$");
            int start = 0;
            while(start!=-1)
            {
                start = s.IndexOf("<img");//获取<img的起始索引
                //Console.WriteLine(start);0,6,6,-1
                if (start != -1)
                {
                    int startindex = s.IndexOf("<img");
                    int endindex = s.IndexOf("$", startindex);
                    //Console.WriteLine(endindex);//7,13,13
                    s = s.Remove(startindex, endindex - startindex + 1);
                }//删除指定的字符串元素
            }
            s = s.Replace("$","</br>");
            Console.WriteLine(s);
            Console.ReadKey();*/

 //C#截取字符串
           /* string str = "2013-10-1 10:10:10*456*654*2013-10-2";
            int index1 = str.IndexOf("*");
            string str1 = str.Substring(0,index1);
            int index3 = str.LastIndexOf("*");
            string str3 = str.Substring(index3+1);
            int index2 = index1 + 1;
            string str2 = str.Substring(index2,index3-index2);
            Console.WriteLine(str1);
            Console.WriteLine(str3);
            Console.WriteLine(str2);
            Console.ReadKey();*/

 //截取长度为8的字符串
           /* string str = "从数据库中截取的新闻标题练习题";
            string str1 = "";
            if (str.Length > 8)
            {
                str1 = str.Substring(0, 8);
            }
            else
            {
                str1 = str;
            }
            Console.WriteLine(str1);
            Console.ReadKey();*/

 //截取第一个s和第一个n之间的字符串
           /* string str = "ennnnnglisssssh";
            string stn = str.Substring(str.IndexOf("n")+1,str.IndexOf("s")-str.IndexOf("n"));
            Console.WriteLine(stn);
            Console.ReadKey();*/
            //分割存储用户信息的字符串
           /* string userinfo = "userid=1&username=admin&password=admin123";
            char[] charList = {'&','=' };
            string[] userlist = userinfo.Split(charList);
            Console.WriteLine("用户编号:{0}",userlist[1]);
            Console.WriteLine("用户名:{0}",userlist[3]);
            Console.WriteLine("用户密码:{0}",userlist[5]);
            Console.ReadKey();*/
            //把字符串中不同字符分开的数据取出来

 /*string mystring = "12323443,344533;34556654 45564345";
            char[] char_list = new char[] {',',';',' ' };
            string[] mystr = mystring.Split(char_list);
            foreach(string str in mystr)
            {
                if(str!="")
                {
                    Console.WriteLine(str);
                }  
            }
            Console.ReadKey();*/

  //
            //不考虑大小写的方法
           /* string str = "123asd";
            string str1 ="123AAAsd";
            if (string.Compare(str, str1, true) > 0)
            {
                Console.WriteLine("two eques is true");
            }
            else if (string.Compare(str, str1, true) < 0)
            {
                Console.WriteLine("str<str1");
            }
            else
            {
                Console.WriteLine("str>str1");
            }
            Console.ReadKey();*/
            //string and stringbuilder的执行效率哪个更高
            //StringBuilder的执行效率高
            //
            //Format格式化
           // 正则表达式问题

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2014-02-15 16:18  小马哥~程序之美  阅读(217)  评论(0编辑  收藏  举报

导航