What is the fastest way to replace extra white spaces to one white space?
e.g.
from
foo bar
to
foo bar
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The fastest way? Iterate over the string and build a second copy in a
StringBuildercharacter by character, only copying one space for each group of spaces.The easier to type
Replacevariants will create a bucket load of extra strings (or waste time building the regex DFA).Edit with comparison results:
Using http://ideone.com/NV6EzU, with n=50 (had to reduce it on ideone because it took so long they had to kill my process), I get:
Which is indeed as expected,
Regexis horribly inefficient for something this simple.