Concatenation/joining strings together in a more readable way

Marcone marcone at email.com
Tue Dec 31 09:32:48 UTC 2019


On Monday, 30 December 2019 at 14:56:59 UTC, mipri wrote:
> On Monday, 30 December 2019 at 10:23:14 UTC, Marcone wrote:
>> On Monday, 30 December 2019 at 09:41:55 UTC, mipri wrote:
>
>>> 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.
>
> It doesn't work the same way. These are not the same:
>
>   Helo name {}
>   Helo {} name
>
> Python's implementation doesn't get confused if your format()
> arguments include a {}.

Try this:

import std: Format = format;

string format(T...)(T text){return text[0].replace("{}", 
"%s").Format(text[1..$]);}


More information about the Digitalmars-d-learn mailing list