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. Remove multiple blanks in a string?

Remove multiple blanks in a string?

Scheduled Pinned Locked Moved C#
regextutorialquestionlearning
11 Posts 5 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.
  • S Syntaxo

    Hi! I need a good method to remove multiple blanks from a string and replace them with a single blank. For example: "This   is   a  string" -> "This is a string". The method I am using now works fine but is a bit resource demanding (about 80% of the thread). :| public static string RemoveMb (string strIn) // Remove multiple blanks { string search = @"\s+"; string replace = @" "; strIn = Regex.Replace(strIn, search, replace); return strIn; } Anyone know of a better method? :rose: Best regards, Daniel

    L Offline
    L Offline
    lainoo
    wrote on last edited by
    #2

    public string st(string st) { for(string st1="";st!=st1;st1=st,st=st1.Replace(" "," ")); return st; } Sorry ...

    S 1 Reply Last reply
    0
    • L lainoo

      public string st(string st) { for(string st1="";st!=st1;st1=st,st=st1.Replace(" "," ")); return st; } Sorry ...

      S Offline
      S Offline
      Syntaxo
      wrote on last edited by
      #3

      Hmm, I took a quick look at and even tested that code. Does it do anything good at all? :confused: Doesn't it just replace all blanks (even the multiple ones) with a "new" blank? Best regards, Daniel

      M 1 Reply Last reply
      0
      • S Syntaxo

        Hmm, I took a quick look at and even tested that code. Does it do anything good at all? :confused: Doesn't it just replace all blanks (even the multiple ones) with a "new" blank? Best regards, Daniel

        M Offline
        M Offline
        mav northwind
        wrote on last edited by
        #4

        Come on, take a look at the proposal and use your brain. It's not so hard to find out that the first string in Replace has to consist of two blanks and that they're rendered as one blank because lainoo didn't quote his code in <pre>. mav

        S 1 Reply Last reply
        0
        • S Syntaxo

          Hi! I need a good method to remove multiple blanks from a string and replace them with a single blank. For example: "This   is   a  string" -> "This is a string". The method I am using now works fine but is a bit resource demanding (about 80% of the thread). :| public static string RemoveMb (string strIn) // Remove multiple blanks { string search = @"\s+"; string replace = @" "; strIn = Regex.Replace(strIn, search, replace); return strIn; } Anyone know of a better method? :rose: Best regards, Daniel

          C Offline
          C Offline
          Corinna John
          wrote on last edited by
          #5

          //split string
          String[] parts = strIn.split(' ');

          //re-build string
          StringBuilder newString = new StringBuilder();
          for(int n=0; n

          _________________________________
          Vote '1' if you're too lazy for a discussion

          S 1 Reply Last reply
          0
          • M mav northwind

            Come on, take a look at the proposal and use your brain. It's not so hard to find out that the first string in Replace has to consist of two blanks and that they're rendered as one blank because lainoo didn't quote his code in <pre>. mav

            S Offline
            S Offline
            Syntaxo
            wrote on last edited by
            #6

            Well, I guess it wasn't that hard to find out so I'll accept the heat for that one. :-O But it still doesn't work since the "multiple blanks" in my strings can consist of more than just two blanks. Best regards, Daniel

            M 1 Reply Last reply
            0
            • C Corinna John

              //split string
              String[] parts = strIn.split(' ');

              //re-build string
              StringBuilder newString = new StringBuilder();
              for(int n=0; n

              _________________________________
              Vote '1' if you're too lazy for a discussion

              S Offline
              S Offline
              Syntaxo
              wrote on last edited by
              #7

              Can't get that to work since it seems to split directly after the first blank found and leaving the remaining blanks in the string-array items :(( Best regards, Daniel

              1 Reply Last reply
              0
              • S Syntaxo

                Well, I guess it wasn't that hard to find out so I'll accept the heat for that one. :-O But it still doesn't work since the "multiple blanks" in my strings can consist of more than just two blanks. Best regards, Daniel

                M Offline
                M Offline
                mav northwind
                wrote on last edited by
                #8

                Did you try the code after correcting it? It's supposed to replace double spaces with single spaces until the string doesn't change anymore. Each run should halve the double spaces in your input string until there are only single spaces. mav

                S 1 Reply Last reply
                0
                • M mav northwind

                  Did you try the code after correcting it? It's supposed to replace double spaces with single spaces until the string doesn't change anymore. Each run should halve the double spaces in your input string until there are only single spaces. mav

                  S Offline
                  S Offline
                  Syntaxo
                  wrote on last edited by
                  #9

                  Ahh! Tested it again now more thoroughly. My deepest apologies to both lainoo and You mav, the code works it just don't apply to tabs (\t) which the regexp "\s" seem to do. :-O I got confused when I saw the big gaps in my string but when looked at closely they are mixed blanks and tabs. The method works way faster than the regular expression I used, now I need to find a way to include tabs. I guess I'll have to go with a double run to first remove all tabs? Thanks a lot! Best regards, Daniel

                  M 1 Reply Last reply
                  0
                  • S Syntaxo

                    Ahh! Tested it again now more thoroughly. My deepest apologies to both lainoo and You mav, the code works it just don't apply to tabs (\t) which the regexp "\s" seem to do. :-O I got confused when I saw the big gaps in my string but when looked at closely they are mixed blanks and tabs. The method works way faster than the regular expression I used, now I need to find a way to include tabs. I guess I'll have to go with a double run to first remove all tabs? Thanks a lot! Best regards, Daniel

                    M Offline
                    M Offline
                    mav northwind
                    wrote on last edited by
                    #10

                    Nice to see you were persistent enough to actually try :) I think a simple call to

                    st = st.Replace("\t"," ");
                    

                    before the loop does the trick. You don't have to care about double (or more) tabs, since the resulting double (or more) blanks are taken care of in the loop anyway. mav

                    1 Reply Last reply
                    0
                    • S Syntaxo

                      Hi! I need a good method to remove multiple blanks from a string and replace them with a single blank. For example: "This   is   a  string" -> "This is a string". The method I am using now works fine but is a bit resource demanding (about 80% of the thread). :| public static string RemoveMb (string strIn) // Remove multiple blanks { string search = @"\s+"; string replace = @" "; strIn = Regex.Replace(strIn, search, replace); return strIn; } Anyone know of a better method? :rose: Best regards, Daniel

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

                      Create a static instance of the Regex object with Compiled flags, and reuse that object to do the replace. Much of the processing is used when the method u are calling is being constructed internally. top secret
                      Download xacc-ide 0.0.3 now!
                      See some screenshots

                      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