Concatenation/joining strings together in a more readable way
Marcone
marcone at email.com
Mon Dec 30 10:23:14 UTC 2019
On Monday, 30 December 2019 at 09:41:55 UTC, mipri wrote:
> On Monday, 30 December 2019 at 06:47:37 UTC, Marcone wrote:
>> Use Python format() style:
>>
>> import std;
>> import std: Format = format;
>>
>> // format()
>> string format(T...)(T text){
>> string texto = text[0];
>> foreach(count, i; text[1..$]){
>> texto = texto.replaceFirst("{}", to!string(i));
>> texto = texto.replace("{%s}".Format(to!string(count)),
>> to!string(i));
>> }
>> return texto;
>> }
>
> This leaks too much.
>
> writeln("Helo {} {}".format("xx", "name")); // Helo xx name
> writeln("Helo {} {}".format("{}", "name")); // Helo name {}
This function replace {} for arguments received. You just need
don't send {} as arguments.
I tested native function format() in Python:
print("Helo {} {}".format("{}", "name")) # Helo {} name
Nothing wrong, working same way.
Works with index too.
writeln("Hi, my name is {1} and I live in
{0}.".format("Brasil", "Marcone"));
writeln("Hi, my name is {} and I live in {}.".format("Marcone",
"Brasil"));
More information about the Digitalmars-d-learn
mailing list