With Mathematica I always feel that strings are "second class citizens." Compared to a language such as PERL, one must juggle a lot of code to accomplish the same task.
The available functionality is not bad, but the syntax is uncomfortable. While there are a few shorthand forms such as <>
for StringJoin
and ~~
for StringExpression
, most of the string functionality lacks such syntax, and uses clumsy names like: StringReplace
, StringDrop
, StringReverse
, Characters
, CharacterRange
, FromCharacterCode
, and RegularExpression
.
In Mathematica strings are handled like mathematical objects, allowing 5 "a" + "b"
where "a"
and "b"
act as symbols. This is a feature that I would not change, even if that would not break stacks of code. Nevertheless it precludes certain terse string syntax, wherein the expression 5 "a" + "b"
would be rendered "aaaaab"
for example.
What is the best way to make string manipulation more convenient in Mathematica?
Ideas that come to mind, either alone or in combination, are:
Overload existing functions to work on strings, e.g.
Take
,Replace
,Reverse
.- This was the original topic of my question to which Sasha replied. It was seen as inadvisable.
Use shortened names for string functions, e.g.
StringReplace
>>StrRpl
,Characters
>>Chrs
,RegularExpression
>> "RegEx"Create new infix syntax for string functions, and possibly new string operations.
Create a new container for strings, e.g.
str["string"]
, and then definitions for various functions. (This was suggested by Leonid Shifrin.)A variable of (4), expand strings (automatically?) to characters, e.g.
"string"
>>str["s","t","r","i","n","g"]
so that the characters can be seen byPart
,Take
, etc.Call another language such as PERL from within Mathematica to handle string processing.
Create new string functions that conglomerate frequently used sequences of operations.