MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: StringJoin with ToString

Dear All,

I've considered your warnings, but - mainly because of my curiosity -
I continued to find a solution to the problem. Combining information
from here and my earlier tries, I've managed to come up with a stable
code that is quite short:

toString[expr_String] := expr;
toString[expr_] := ToString[expr];

Attributes[System`StringJoin] = {};
System`StringJoin[expr___] := StringJoin[{expr}];
System`StringJoin[expr_List] := Module[{s = ""}, Scan[(s = StringInsert
[s, toString[#], -1]) &, expr]; s]

Extensive testing shows that this works as expected (error messages
are returned unchanged, Import[file, "Lines"] works correctly, etc.).
Of course, there are effects, which must be considered, e.g. David's

ss = Quiet[2 <> 3];
Map[ToString[#^2] &, ss]

this indeed would give 23 instead of 49, but that is definitely not
disrupting, since one who uses the new StringJoin *should be aware*
that it actually joins non-strings as well. I wouldn't consider it a
problem, just a point, where it must be kept in mind what the extended
StringJoin can do. To make my point clearer: no one would expect the

ss = Quiet[2\[CirclePlus]3];
Map[ToString[#^2] &, ss]

to result other than "4"\[CirclePlus]"9" IFF there is no assigned
meaning of CirclePlus. Although if one uses CirclePlus to actually do
something, then the result of the first line won't be the same as the
input: 2\[CirclePlus]3. As one would expect.

For the other problematic example of Albert (modified here a bit):

If[Head[Quiet[StringJoin[1, 2, 3]]] === StringJoin, Print["this was no
string!"], Print["string ok"]]

This should (in case of the new StringJoin) result "string ok", while
in case of the original StringJoin: "this was no string!". But the
former is exactly the functionality I would like to gain, namely to
get a string from StringJoin even in case of non-string input.

So, of course one must be cautios, but I wouldn't consider these cases
Could anyone find any abnormal/unexpected behaviour of this new

Thanks for your help,


  • Prev by Date: Re: Help with ScriptSource in GUIKit
  • Next by Date: Re: Importing a large image...
  • Previous by thread: Re: StringJoin with ToString
  • Next by thread: Can nobody help me? Re: selecting a column in a grid