www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Interpolated strings?

reply Andy Valencia <dont spam.me> writes:
I have a string full of JavaScript to serve up, and a couple 
variable values need to be interpolated into it.  I read that 
dlang now has interpolated strings under i"...", so yay!  A bit 
of example code to show what I tried:

string s;
int i
auto x = i"Message $(s) has value $(i)"
writeln(x)

and it worked as expected.  But:

string x = i"Message $(s) has value $(i)"

instead does NOT work.  And there's no toString, nor does 
to!string do the trick.  The core.interpolation file has no 
unittest, so I can't crib from anything there.  How _does_ one 
get the string post-interpolation?

This is with ldc2-1.40.0-beta4-linux-x86_64

Thanks,
Andy Valencia
Dec 16
next sibling parent "H. S. Teoh" <hsteoh qfbox.info> writes:
On Mon, Dec 16, 2024 at 08:33:27PM +0000, Andy Valencia via Digitalmars-d-learn
wrote:
 I have a string full of JavaScript to serve up, and a couple variable
 values need to be interpolated into it.  I read that dlang now has
 interpolated strings under i"...", so yay!  A bit of example code to
 show what I tried:
 
 string s;
 int i
 auto x = i"Message $(s) has value $(i)"
 writeln(x)
 
 and it worked as expected.  But:
 
 string x = i"Message $(s) has value $(i)"
 
 instead does NOT work.  And there's no toString, nor does to!string do
 the trick.  The core.interpolation file has no unittest, so I can't
 crib from anything there.  How _does_ one get the string
 post-interpolation?
[...] i"..." returns a tuple of its constituents; to get a string, use .text. Like this: import std; string x = i"Message $(s) has value $(i)".text; // should work T -- What did the kidnapper have for lunch? Seize-her salad.
Dec 16
prev sibling parent reply Juraj <zero vec4.xyz> writes:
On Monday, 16 December 2024 at 20:33:27 UTC, Andy Valencia wrote:

 string x = i"Message $(s) has value $(i)"
```d import std.conv : text; string x = i"Message $(s) has value $(i)".text; ``` [Documentation](https://dlang.org/spec/istring.html#tostring) Juraj
Dec 16
parent Andy Valencia <dont spam.me> writes:
On Monday, 16 December 2024 at 20:42:45 UTC, Juraj wrote:
 On Monday, 16 December 2024 at 20:33:27 UTC, Andy Valencia 
 wrote:

 string x = i"Message $(s) has value $(i)"
```d import std.conv : text; string x = i"Message $(s) has value $(i)".text; ``` [Documentation](https://dlang.org/spec/istring.html#tostring) Juraj
Thank you (and H.S. Teoh as well). That does the trick nicely. Andy
Dec 16