Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • jeff mayer 122 posts 200 karma points
    Mar 25, 2015 @ 22:40
    jeff mayer
    0

    removing special characters from string

    Trying to strip all special characters from a string by using the following:

    using System.Text.RegularExpressions;
    
    string your_String = CurrentPage.description;
    string my_String =  Regex.Replace(your_String, @"[^0-9a-zA-Z]+", ",");
    

    unfortunately I get the following error:

    The best overloaded method match for system.text.regularexpresions.regex.replace(string,string)' has some invalid arguments. Any suggestions?

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Mar 26, 2015 @ 05:29
    Jan Skovgaard
    2

    Hi Jeff

    It's because you're passing 3 parameters but the the method only accepts two as the error message is also indicating.

    You can see some examples on how to use Regex.Replace here https://msdn.microsoft.com/en-us/library/xwewhkd1%28v=vs.110%29.aspx

    You should be able to do something like

      string pattern =  @"[^0-9a-zA-Z]+";
      string input = CurrentPage.description;
      string replacement = "";
      Regex rgx = new Regex(pattern);
      string result = rgx.Replace(input, replacement)
    
     //Write out the result
    @result
    

    Hope this helps.

    /Jan

  • jeff mayer 122 posts 200 karma points
    Mar 26, 2015 @ 15:38
    jeff mayer
    1

    Thanks Jan

    This is what I ended up using for anyone else that would need this.

       string pattern = @"[^\w\.@:-]";
       string input = Umbraco.StripHtml(CurrentPage.description).ToString();
    
       string replacement = " ";
       Regex rgx = new Regex(pattern);
       string result = rgx.Replace(input, replacement);
    
  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Mar 26, 2015 @ 15:44
    Jan Skovgaard
    0

    Hi Jeff

    Happy I could help - Remember to mark the issue as solved so others can jump straight to the solution if they come across the same issue.

    Happy coding!

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft