如何用正则表达式的Replace将字符串第6位后的内容用空白替换?

解决方案 »

  1.   


    void Main()
    {
    string s="afdsaf111dsa";
    string reuslt=Regex.Match(s,@".{0,6}").Value;
     
    reuslt+=Regex.Replace(s,@".{0,6}([^\s]*)",delegate(Match m){return new string(' ',m.Groups[1].Value.Length);});
     
    Console.WriteLine(reuslt);
    //afdsaf      }
      

  2.   

    为什么不用string 的 Replace?
      

  3.   


    //linq 
    void Main()
    {
    string s="afdsaf111dsa";
    string reuslt=s.Substring(0,6)+ new string(' ',s.ToCharArray().Skip(6).Count());
    Console.WriteLine(reuslt);
    //afdsaf      
    }
      

  4.   

    有多少个字符就补多少个空白?Regex reg = new Regex(@"(?<=^.{6}).+");
    string result = reg.Replace(yourStr, delegate(Match m) { return new string(' ', m.Value.Length); });
    richTextBox2.Text = result;
      

  5.   


    Regex.Replace("123456789", @"((?<start>.{6})(\S*))",@"${start}")
      

  6.   

    string s = "1234567890";
    string result = s.Substring(0, 6) + new string(' ', s.Length - 6);
      

  7.   

    其实用string的方法更简洁yourStr = yourStr.Length >6? yourStr.Substring(0, 6).PadRight(yourStr.Length, ' '):yourStr;