Remove multiple blanks in a string?
-
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
-
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
-
public string st(string st) { for(string st1="";st!=st1;st1=st,st=st1.Replace(" "," ")); return st; } Sorry ...
-
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
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
-
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
//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 -
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
-
//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 -
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
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
-
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
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
-
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
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
-
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
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