4
$\begingroup$

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?", " " -> ""]

IsThisOK,huh?

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

$\endgroup$
0

1 Answer 1

5
$\begingroup$
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?" *)
$\endgroup$
1
  • $\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.