9
$\begingroup$

Both <>, i.e. StringJoin and ~~, i.e. StringExpression, can be used to join two strings. I know that only the latter can be used to describe a string pattern (instead of joining actual strings). Are there other situations when only <> is appropriate? Or can I always use ~~?

$\endgroup$
1
  • 4
    $\begingroup$ StringJoin ignores list structure, so it's a bit more convenient if patterns are not involved, IMO. It's also a bit faster. $\endgroup$ Commented Jun 21, 2021 at 22:44

1 Answer 1

16
$\begingroup$

In my opinion, if you want to join strings, the choice between <> and ~~ is clear: StringJoin is explicitly intended to join strings. StringExpression is not, it just happens to do so in many contexts in the current version, as it facilitates pattern matching.

The documentation makes no promise that StringExpression should work for the purpose of joining strings, so why would want to use it for that at all, especially when there is a different operator which is specifically for joining strings? I think that even if StringExpression always joins strings in the current version (I am not aware of cases when it doesn't), Wolfram would be completely justified in changing this behaviour in the future if it helps improve string pattern matching.


So, to sum up:

When should I use <> instead of ~~ to join two strings?

Always. Use functions for their intended purpose, not for things that they happen to do by accident, but give no guarantees for.


Differences in the current behaviour of StringJoin and StingExpression:

As @ciao points out in a comment, StringJoin works on (nested) lists of strings, while StringExpression does not.

He also mentions that StringJoin may be faster, so I did a quick benchmark. I could not find any speed difference between the two, but the fact that StingJoin can be used on lists does give it a practical performance advantage, as one can avoid the overhead from Apply.

SeedRandom[12345]; list = RandomWord[1000000]; Table[StringJoin[list]; // RepeatedTiming // First, 5] (* {0.0266891, 0.0262008, 0.0269283, 0.0270262, 0.0266054} *) Table[StringJoin @@ list; // RepeatedTiming // First, 5] (* {0.0451839, 0.0451968, 0.045871, 0.0447449, 0.0457315} *) Table[StringExpression @@ list; // RepeatedTiming // First, 5] (* {0.0454046, 0.0450875, 0.0447029, 0.0453249, 0.0450471} *) 
$\endgroup$
3
  • 1
    $\begingroup$ Also you can count on StringJoin issuing a message when given a non-string. $\endgroup$ Commented Jun 22, 2021 at 10:59
  • $\begingroup$ A comment for people participating in Wolfram Challenges: To get the lowest code characters score, you have to use ~~ instead of <>, because the InputForm (which is what the scoring system measures) of ~~ is just ~~, while <> gets converted to StringJoin[...]. $\endgroup$ Commented Mar 11, 2024 at 10:26
  • $\begingroup$ @Domen Worth a separate answer? $\endgroup$ Commented Mar 11, 2024 at 15:59

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.