现有字符串  str_1="YouAreRight";  
需要用正则表达式  实现str_2=" You Are Right";  
 
str_1  转化成str_2  
 
每个大写字母前面增加空格.  
使用C#  正则表达式  实现

解决方案 »

  1.   

    using System;
    using System.Collections;
    using System.Text.RegularExpressions;
    public class MyClass
    {
    public static void Main()
    {
      string str_1="sHjhgsdKkjhLjk";
    Console.WriteLine(str_1+"\n");
    Console.WriteLine(Regex.Replace(str_1,"[A-Z]",new MatchEvaluator(Replace)));
    //new MatchEvaluator(Replace)用委托,对匹配的字符处理
    RL();
    }
    public static string Replace(Match m)//
          {
    return " "+m.Value;
          } #region Helper methods private static void WL(object text, params object[] args)
    {
    Console.WriteLine(text.ToString(), args);
    }

    private static void RL()
    {
    Console.ReadLine();
    }

    private static void Break() 
    {
    System.Diagnostics.Debugger.Break();
    } #endregion
    }
      

  2.   

    哪里复杂了?
    string str_1="sHjhgsdKkjhLjk";//你的字符串
    string str_2= Regex.Replace(str_1,"[A-Z]",new MatchEvaluator(Replace)));//然后在外面定义方法,对匹配的字符处理,返回要你的字符
    //就这么几行代码
    public static string Replace(Match m)//
    {
       return " "+m.Value;
    }