Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: StringJoin with ToString

  • To: mathgroup at smc.vnet.net
  • Subject: [mg98314] Re: StringJoin with ToString
  • From: ADL <alberto.dilullo at tiscali.it>
  • Date: Sun, 5 Apr 2009 06:40:44 -0400 (EDT)
  • References: <gr6cbs$pva$1@smc.vnet.net>

With the following definitions, your idea appears to work (at least in
a few examples I tested):

ClearAll[toString];
toString[x_?NumericQ, format___] := ToString[x, format];
toString[x_[y__], format___] := x @@ (toString /@ {y});
toString[x_Symbol, format___] := ToString[x, format];
toString[x_, format___] := x;

Unprotect[System`StringJoin];
Unprotect[$UseNewStringJoin];
$UseNewStringJoin = True;
System`StringJoin[expr___] :=
  Block[{$UseNewStringJoin = False},
    StringJoin @@ (toString /@ {expr})] /; TrueQ[$UseNewStringJoin];
Protect[$UseNewStringJoin];
Protect[System`StringJoin];

The problem was the way ToString works, which is incompatible with
intermediate formatting functions. The definition of "toString" does
the job.


In[12]:= StringJoin[{1, 2, 3}] // InputForm

Out[12]//InputForm=
"123"

In[13]:= StringJoin[1, a, 3] // InputForm

Out[13]//InputForm=
"1a3"

In[17]:= StringJoin[1, "2", \[Pi]] // InputForm

Out[17]//InputForm=
"12Pi"

In[15]:= Append[dummy, 2]

During evaluation of In[15]:= Append::normal: Nonatomic expression
expected at position 1 in Append[dummy,2]. >>

Out[15]= Append[dummy,2]


ADL


On 4 Apr, 03:15, replicator... at gmail.com wrote:
> It is quite interesting why StringJoin does not apply ToString to
> those arguments which are non-string. I've overridden the built-in
> StringJoin, to save a few unnecessary ToStrings by the standard method
> (the context specifications are necessary for packaging):
>
...
>
> Thanks
>
> Istvan Zachar



  • Prev by Date: Re: Styling the selected Tab's label in TabView
  • Next by Date: Re: passing an array variable as a file name string to FindList
  • Previous by thread: Re: StringJoin with ToString
  • Next by thread: StringJoin with ToString