Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Stripping Punctuation

Stripping Punctuation

Scheduled Pinned Locked Moved C#
comquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MStanbrook
    wrote on last edited by
    #1

    Anyone have an "efficient" routine for stripping punctuation for a string? Mike Stanbrook mstanbrook@yahoo.com

    N 1 Reply Last reply
    0
    • M MStanbrook

      Anyone have an "efficient" routine for stripping punctuation for a string? Mike Stanbrook mstanbrook@yahoo.com

      N Offline
      N Offline
      Nnamdi Onyeyiri
      wrote on last edited by
      #2

      public string StripPunctuation(string source)
      {
      // any chars you want to keep, put between the [] brakets.
      // i think the ^ has to remain where it is though.
      string pattern = "[^a-zA-Z ]";

      // use regular expressions.
      System.Text.RegularExpressions.Regex regex;
      regex = new System.Text.RegularExpressions.Regex(pattern);
      return regex.Replace(source, "");
      }

      That should do it. I think .NET's regular expression classes are effiecient enough.

      1001111111011101111100111100101011110011110100101110010011010010
      Sonork | 100.21142 | TheEclypse

      D 1 Reply Last reply
      0
      • N Nnamdi Onyeyiri

        public string StripPunctuation(string source)
        {
        // any chars you want to keep, put between the [] brakets.
        // i think the ^ has to remain where it is though.
        string pattern = "[^a-zA-Z ]";

        // use regular expressions.
        System.Text.RegularExpressions.Regex regex;
        regex = new System.Text.RegularExpressions.Regex(pattern);
        return regex.Replace(source, "");
        }

        That should do it. I think .NET's regular expression classes are effiecient enough.

        1001111111011101111100111100101011110011110100101110010011010010
        Sonork | 100.21142 | TheEclypse

        D Offline
        D Offline
        David Stone
        wrote on last edited by
        #3

        Why not just rewrite:

        string pattern = "[^a-zA-Z ]";
        System.Text.RegularExpressions.Regex regex;
        regex = new System.Text.RegularExpressions.Regex(pattern);
        return regex.Replace(source, "");

        As

        Regex regex = new Regex("[^a-zA-z]");
        regex.Replace(source, "");

        Makes things a lot simpler if you skip the first string and move the declaration and instantiation on to one line.


        I don't know whether it's just the light but I swear the database server gives me dirty looks everytime I wander past. -Chris Maunder Microsoft has reinvented the wheel, this time they made it round. -Peterchen on VS.NET

        L 1 Reply Last reply
        0
        • D David Stone

          Why not just rewrite:

          string pattern = "[^a-zA-Z ]";
          System.Text.RegularExpressions.Regex regex;
          regex = new System.Text.RegularExpressions.Regex(pattern);
          return regex.Replace(source, "");

          As

          Regex regex = new Regex("[^a-zA-z]");
          regex.Replace(source, "");

          Makes things a lot simpler if you skip the first string and move the declaration and instantiation on to one line.


          I don't know whether it's just the light but I swear the database server gives me dirty looks everytime I wander past. -Chris Maunder Microsoft has reinvented the wheel, this time they made it round. -Peterchen on VS.NET

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          David Stone wrote: Regex regex = new Regex("[^a-zA-z]");regex.Replace(source, ""); or return new Regex("[^a-zA-z]").Replace(source, ""); :) WebBoxes - Yet another collapsable control, but it relies on a "graphics server" for dynamic pretty rounded corners, cool arrows and unlimited font support.

          D 1 Reply Last reply
          0
          • L leppie

            David Stone wrote: Regex regex = new Regex("[^a-zA-z]");regex.Replace(source, ""); or return new Regex("[^a-zA-z]").Replace(source, ""); :) WebBoxes - Yet another collapsable control, but it relies on a "graphics server" for dynamic pretty rounded corners, cool arrows and unlimited font support.

            D Offline
            D Offline
            David Stone
            wrote on last edited by
            #5

            Bah, it's all the same IL-wise. :-D


            I don't know whether it's just the light but I swear the database server gives me dirty looks everytime I wander past. -Chris Maunder Microsoft has reinvented the wheel, this time they made it round. -Peterchen on VS.NET

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups