1

Say I have a string {100 aa bbc%j%b%wa ajjcj11j0%2wj fc} with a lot of undesired characters or spaces in it, for example: {" ", "%", "w", "j"}. I'd like to strip the string of these characters, with the provided example giving us the output string:

testString = "100 aa bbc%j%b%wa ajjcj11j0%2wj fc";
StringReplace[testString, {" ", "%", "w", "j"} -> ""]

out: "100aabbcbaac1102fc";

Now, say we don't have a convenient list of undesired characters / spaces / paragraph indicators / etc. Is there a way to select for certain characters instead of specifying that certain characters should be deleted (as in the example)?

Bob2Alice
  • 127
  • 1
  • 1
  • 7
  • @b.gatessucks Ah, you have to double click to select the full alphanumeric string, and I wasn't paying enough attention! – Bob2Alice Apr 01 '14 at 07:36

2 Answers2

2
testString = "100 aa bbc%j%b%wa ajjcj11j0%2wj fc";
StringJoin@StringCases[testString, {"a", "j", "c", " "}]

(* aa cja ajjcjjj c *)

The second argument for StringCases can use character ranges, etc.

As to "bulk" but specific upcase/downcase, something like:

StringReplace[%, (# -> ToUpperCase[#]) & /@ {"c", "j"}]

(*  aa CJa aJJCJJJ C *)
ciao
  • 25,774
  • 2
  • 58
  • 139
2

Another way using RegularExpression

StringReplace[testString, RegularExpression["[^ajc ]"] :> ""]

Gives:

 aa cja ajjcjjj c
RunnyKine
  • 33,088
  • 3
  • 109
  • 176