In a string, I want to remove only the single-character whitespace between printing characters, leaving any multiple, contiguous, whitespace characters between printing characters unchanged?

What is needed is:

f["Is This OK,  huh?"]
IsThisOk, huh?   

StringReplace hasn't worked for me because I can't see how to restrict it to single-character whitespace.

StringReplace["Is This OK,  huh?", " " -> ""]


My string comes from Import["url"] and has a length in excess of 250,000 characters.


1 Answer 1

StringReplace["Is This OK,  huh?", 
   {w : Repeated[WhitespaceCharacter, {2, Infinity}] :> w, WhitespaceCharacter -> ""}]
(* IsThisOK,  huh? *)

Note: If you also want to replace multiple contiguous white spaces with a single white space character, you can do

StringReplace["Is This OK,         huh?",
 {Repeated[WhitespaceCharacter, {2, Infinity}] -> " ",  WhitespaceCharacter -> ""}]
(* "IsThisOK, huh?" *)
  • $\begingroup$ Myyyyyyyy bad. I was making my own question here :P $\endgroup$
    – Öskå
    Oct 24, 2014 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.