+ Reply to Thread
Results 1 to 2 of 2

Thread: remove specific characters from string

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    14

    remove specific characters from string

    I have list of strings in C#:
    Code:
    List<string> lstNames = new List<string>();
    lstNames.add("TRA-94:23");
    lstNames.add("TRA-42:101");
    lstNames.add("TRA-109:AD");
    
    foreach (string n in lstNames) {
      // logic goes here that somehow uses regex to remove all special characters
      string regExp = "NO_IDEA";
      string tmp = Regex.Replace(n, regExp, "");
    }
    I need to be able to loop over the list and return each item without any special characters. For example, item one would be "TRA9423", item two would be "TRA42101" and item three would be TRA109AD.

    Is there a regular expression that can accomplish this for me?

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    14
    I find that a whitelist rather than a blacklist is the best approach in most situations:
    Code:
    tmp = Regex.Replace(n, "[^0-9a-zA-Z]+", "");

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts