首页 > 学院 > 开发设计 > 正文

利用正则表达式来反转一句话,以单词为单位

2019-11-18 16:55:11
字体:
来源:转载
供稿:网友

 public static int Main(string[] args)
        {
   

            Console.WriteLine("============================");
            string s = "i'm a student,my name is yuhong!ok!!!";
 
            string[] sArray = System.Text.RegularExPRessions.Regex.Split(s, @"[^/w]+", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            //记录标点符号
            string[] sArray2 = System.Text.RegularExpressions.Regex.Split(s, @"[/w]+",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            int start = 0;
            int end = sArray.Length - 1;
            while (start<end)
            {
                string tmp = sArray[start];
                sArray[start] = sArray[end];
                sArray[end] =tmp;
                start++;
                end--;
            }


            start = 0;
            end = sArray2.Length - 1;
            while (start < end)
            {
                string tmp = sArray2[start];
                sArray2[start] = sArray2[end];
                sArray2[end] = tmp;
                start++;
                end--;
            }
            // s=string.Join(" ", sArray);
            string result = "";
            for (int i2 = 0; i2 < sArray.Length; i2++)
            {
                result += sArray[i2] + sArray2[i2];
            }


            s = result;
            Console.WriteLine(s);
            //Console.WriteLine("============================");
            //string str1 = "我**是*****一*****个*************教*****师";
            //string[] str2 = System.Text.RegularExpressions.Regex.Split(str1, @"[*]+");
            //foreach (string iss in str2)
            //    Console.WriteLine(iss.ToString());
            return 0;
        }
http://www.VEVb.com/meiwn/archive/2006/10/19/533587.html


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表