您的位置:首页 > 编程语言 > C#

初识c#---字符串string和正则表达式

2017-09-07 22:31 387 查看

学习字符串:

  1.获取字符串的长度

  2.字符串中查找字符串

  3.截取制定范围内的字符串

  4.替换字符串

  5.插入字符串

  6.判断字符串以什么结束

 

实例:

char[] chars = { 'a', 'b', 'l', 'e' };

    String s = new String(chars);

    Console.WriteLine(s);

    string s1 = "H. ello";

    //无论英文,特殊符号,还是中文都是一个字节

    Console.WriteLine("字符串长度:{0}",s1.Length);

    string s2 = "seggdhdrhe";

    //int index=s2.IndexOf("d");

    //indexOf如果能找打则返回字符所在的索引,找不到返回-1

    int index = s2.IndexOf("h,3,2");

    Console.WriteLine(index);

    练习

    zhaoning@lanou3g.com判断邮箱是否合法

    string s = "zhaoning@lanou3g.com";

    int index1 = s.IndexOf("@");

    Console.WriteLine("@的索引位置为{0}",index1);

    int index2 = s.IndexOf(".");

    Console.WriteLine(".的索引位置为{0}", index2);

    if (index1==-1||index2 ==-1)

    {

        Console.WriteLine("邮箱输入有误");

    }

    else if (index1<index2)

    {

        Console.WriteLine("邮箱合法!");

    }

    else

    {

        Console.WriteLine("邮箱不合法");

    }

    string s3 = "中国,我的母亲!";

    string newstr = s3.Substring(3, 4);

   Console.WriteLine(newstr);

    string s4 = "你好吗,傻货";

    string newstr2 = s4.Replace("傻货", "**");

   Console.WriteLine(newstr2);

    string s5 = "你好,中国";

    string newstr3 = s5.Insert(0, "Lucy:");

    Console.WriteLine(newstr3);

    if(s5.EndsWith("国"))

     {

        Console.WriteLine("以国字结尾");

     }

    string s = "2017年8月10日";

    string[] strs = s.Split('月');

    string[] strs1 = s.Split('年', '月', '日');

    string temp = "";

    for (int i = 0; i<strs1.Length; i++)

    {

        temp = temp + strs1[i];

    }

    Console.WriteLine(temp);

    Console.WriteLine(strs1[0]);

    Console.WriteLine(strs1[1]);

    Console.WriteLine(strs1[2]);

    有一段文本,“Because of you I never stray to far from the sidewalk

    Because of you I learned to play on the safe side ”       

    要求:1.将文本中的所有you 换成 lanou, to 换成 too。                                       

    2.统计出此段文本中有多少个单词;

    string s6 = "Because of you I never stray to far from the sidewalk Because of you I learned to play on the safe side";

    string newstr6 = s6.Replace("you", "lanou");

    string s7 = newstr6;

    string newstr7 = s7.Replace("to", "too");

    Console.WriteLine(newstr7);

    string[] str7 = newstr7.Split(' ');

    Console.WriteLine(str7.Length);

    //判断一个字符串中是否包含数字

    string s8 = "oaffa8sfaaf";           

    for (int i = 0; i<s8.Length; i++)

    {

       if (s8[i]>=48 && s8[i] <= 57)

        {

            Console.WriteLine("包含数字" );

            break;

        }               

    }

    //s="1--2--3--4"

    string s9 = "1--2--3--4";

   string[] newstr = s9.Split('-', '-');

    string tem = "";

    for (int i = 0; i<newstr.Length; i++)

    {

        tem = tem + newstr[i];

    }

    Console.WriteLine(tem);

    string s10 = "aBCD";

    string news10 = s10.ToUpper();//s10.Replace("BCD", "bcd");

    string news11 = s10.ToLower();//s10.Replace("a", "A");

    Console.WriteLine(news10);

    Console.WriteLine(news11);

    15858259565判断手机号

    string str1 = "--158fffffff";

    Console.WriteLine(str1.TrimStart('-','-'));//158

    Console.WriteLine(str1.LastIndexOf('f'));//输出最后一个f的索引值

 

using System.Text.RegularExpressions;//用来使用正则表达式

  正则表达式:

  常用来检查,检索,校验或者替换文本

  正则表达式支持所有平台:

  比如java PHP C# C++等等

  元字符

  正则表达式语言由两种基本字符组成,原义(正常)文本字符和元字符

  不同的元字符代表的含义是不同的,元字符的作用是让正则表达式具有处理字符串的能力

  常见的元字符:

 ^   表示以什么什么开头

 $   表示以什么什么结尾

 * 表示0 or more

 ? 表示0 or 1

 +   表示1 or more

 \w 表示字母, 数字, 下划线, 中文任意字符

 \d 表示数字

 \D 表示非数字

 \s 表示字符串

 \S 表示非空字符串

 [] 组合

 [\s\S] 表示任意字符

 [\s\S]* 表示组合内部出现0次或多次

 [a-z] 表示区间内任意字符

 [0-9] 表示区间内任意数字

 \u4e00-\u9fa5 表示中文

 |      表示逻辑或

 () 表示分组

 {n,m}  表示最少匹配n次,最多匹配m次

 {n,}   表示最少匹配n次

 {n}    表示匹配n次

 [^X]   表示出了X外任意字符(X只是一个代表)

正则表达式实例:

           演示在开始处拼接

            string s1 = "同志们好";

            string result = null;

            result=Regex.Replace(s1, "^", "首长说:");

            Console.WriteLine(result);          

            //在末尾处添加.            

            result = Regex.Replace(result, "$", ".");

            Console.WriteLine(result);

            验证用户输入的字符串,是否满足我们要求的格式

            必须以数字开头, 中文结尾

             string s = "0095645ADA次";

             // ^\s $\u4e00-\u9fa5

             string pattern = @"^\d\w*[\u4e00-\u9fa5]$";

            if(Regex.IsMatch(s, pattern))//Regex.IsMatch是否匹配到 返回bool值

             {

                 Console.WriteLine("合法");

             }

            验证邮箱是否合法

            string s2 = "zhaoning@lanou3g.com";

           // string pattern = @"\w+@\w+(\.\w+)+$";   

            string pattern = @"\w+@\w+(\.\w)+$";       

            if(Regex.IsMatch(s2, pattern))

            {

                Console.WriteLine("合法");

            }

            ()[]

            (0-9a-z)表示匹配0-9 a-z

            [0 - 9a - z]+

            {}  表示长度

            匹配身份证号 15或18位

            18位全数字

            17位数字加1个X

            string id = "11111111111111111X";

           string pattern = @"(^[1-9][0-9]{16}X$)|(^[1-9][0-9]{17}$)|(^[1-9][0-9]{14}$)";

            if (Regex.IsMatch(id, pattern))

            {

                Console.WriteLine("合法");

            }

            else

            {

                Console.WriteLine("不合法");

            }

            匹配电话号码

            第一位1 第二位3 5 7 8

            string tel = "15346484561";

            string pattern1 = @"^[1][3578][0-9]{9}$";

            if (Regex.IsMatch(tel, pattern1))

            {

                Console.WriteLine("合法");

            }

            微博抽取话题

            string str = "#1024##das#天啊#,好恐怖啊,吓死啦 @刘德华";

            string pattern = @"#[\w]+#";

            string pattern = @"#[0-9a-z\u4e00-\u9fa5]+#";

    if (Regex.IsMatch(str, pattern))

    {

        Console.WriteLine("合法");

    }

    MatchCollection mc = Regex.Matches(str, pattern);

    Console.WriteLine(mc.Count);

            foreach (Match item in mc)

            {

                Console.WriteLine(item);

            }

      string pattern1 = @" @\w+";

      MatchCollection mc1 = Regex.Matches(str, patt
9b72
ern1);

     Console.WriteLine(mc1.Count);

            foreach (Match item in mc1)

            {

                Console.WriteLine(item);

            }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐