Detect Mathematica operators eg. \[CircleTimes] in strings

I’m trying to detect all Mathematica operators/symbols (eg. [CircleTimes], [Colon], etc.) in strings so I can replace them with something else, how can I achieve that?

=================

1

 

Wouldn’t StringReplace do this job ?
– Sektor
Aug 19 ’15 at 15:03

1

 

Not at first sight, but thanks to you I’ve got another idea…
– Ranza
Aug 19 ’15 at 15:05

=================

2 Answers
2

=================

You can directly match specific operators as:

s = “abc\[CircleTimes]def”
StringReplace[s, “\[CircleTimes]” -> “CircleTimes”]

“abcCircleTimesdef”

It seems not too unreasonable to manually create a list of all the operators you need to handle.

this builds a list of rules for every possible “\[..]” character

allspecial = (#[[1]] -> StringTake[#[[2]], {4, -3}]) & /@
Select[ Table[ {#, ToString@FullForm@#} &@
FromCharacterCode[i] , {i, 65535}] ,
StringMatchQ[ #[[2]], “\”\\[” ~~ ___ ~~ “]\””] &];
s = “abc\[CircleTimes]def\[ScriptCapitalX]ghi”
StringReplace[s, allspecial]

“abcCircleTimesdefScriptCapitalXghi”

  

 

I need to replace all the [ScriptCapitalX] letters as well (X can be any letter). So this is not perfect as well.
– Ranza
Aug 19 ’15 at 15:31

  

 

OMG, this is it! Thank you!!
– Ranza
Aug 21 ’15 at 8:46

This worked:

StringReplace[ToString@FullForm@”\[CircleTimes]”, (“\\[” ~~ Shortest[c__] ~~ “]”) :> c]

but there’s a drawback – FullForm changes the string also in other places, for instance where a slash “\” exists, so another solution would be welcomed…