Thread overview |
---|
December 25, 2019 Concatenation/joining strings together in a more readable way | ||||
---|---|---|---|---|
| ||||
Are there any other ways to join two strings without Tilde ~ character? I can't seems to find anything about Tilde character concatenation easily, nor the alternatives to it. Can someone share some knowledge on this or at least point out useful links/resources? |
December 25, 2019 Re: Concatenation/joining strings together in a more readable way | ||||
---|---|---|---|---|
| ||||
Posted in reply to BoQsc | On Wednesday, 25 December 2019 at 12:39:08 UTC, BoQsc wrote: > Are there any other ways to join two strings without Tilde ~ character? > I can't seems to find anything about Tilde character concatenation easily, nor the alternatives to it. Can someone share some knowledge on this or at least point out useful links/resources? Tilde operator is documented under cat expression: https://dlang.org/spec/expression.html#cat_expressions An alternative would be std.algorithm.joiner: https://dlang.org/phobos/std_algorithm_iteration.html#.joiner |
December 25, 2019 Re: Concatenation/joining strings together in a more readable way | ||||
---|---|---|---|---|
| ||||
Posted in reply to BoQsc | On Wednesday, 25 December 2019 at 12:39:08 UTC, BoQsc wrote: > Are there any other ways to join two strings without Tilde ~ > character? > I can't seems to find anything about Tilde character concatenation easily, nor the alternatives to it. Can someone > share some knowledge on this or at least point out useful > links/resources? Huh? For clarity I'm going to respond to some potential rewrites of your question. > I don't think "foo" ~ "bar" is very readable. Can I use a > different syntax? No. That's the syntax. Even if you customize it in your own code, by overloading addition or the like, you'll still come across it in other people's code. So I can only recommend that you find some way to make it readable for you. Maybe use a different or larger font? Maybe just get more familiar with it? I don't think the syntax is likely to change. > Where can I find documentation for the ~ operator? It's under 'expressions' in the spec: https://dlang.org/spec/expression.html#cat_expressions > Is ~ the best way to join strings together? Can I use > something else? You can use it to join arrays in general, including strings. Depending on your use case you might prefer joiner() https://dlang.org/phobos/std_algorithm_iteration.html#.joiner in usage like assert(["many", "strings"].joiner("").array == "manystrings"); Or Appender, for many mutating appends to the same array: https://dlang.org/phobos/std_array.html#Appender |
December 30, 2019 Re: Concatenation/joining strings together in a more readable way | ||||
---|---|---|---|---|
| ||||
Posted in reply to mipri | On Wednesday, 25 December 2019 at 13:07:44 UTC, mipri wrote: > On Wednesday, 25 December 2019 at 12:39:08 UTC, BoQsc wrote: >> Are there any other ways to join two strings without Tilde ~ >> character? >> I can't seems to find anything about Tilde character concatenation easily, nor the alternatives to it. Can someone >> share some knowledge on this or at least point out useful >> links/resources? > > Huh? > > For clarity I'm going to respond to some potential rewrites of > your question. > >> I don't think "foo" ~ "bar" is very readable. Can I use a >> different syntax? > > No. That's the syntax. Even if you customize it in your own > code, by overloading addition or the like, you'll still come > across it in other people's code. So I can only recommend that > you find some way to make it readable for you. Maybe use a > different or larger font? Maybe just get more familiar with it? > > I don't think the syntax is likely to change. > >> Where can I find documentation for the ~ operator? > > It's under 'expressions' in the spec: > > https://dlang.org/spec/expression.html#cat_expressions > >> Is ~ the best way to join strings together? Can I use >> something else? > > You can use it to join arrays in general, including strings. > Depending on your use case you might prefer joiner() > > https://dlang.org/phobos/std_algorithm_iteration.html#.joiner > > in usage like > > assert(["many", "strings"].joiner("").array == "manystrings"); > > Or Appender, for many mutating appends to the same array: > > https://dlang.org/phobos/std_array.html#Appender 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; } void main() { string name = "Marcone"; writeln("Helo {}".format(name)); // Helo Marcone writeln("Helo {0}".format(name)); // Helo Marcone } |
December 30, 2019 Re: Concatenation/joining strings together in a more readable way | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | 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 {}
|
December 30, 2019 Re: Concatenation/joining strings together in a more readable way | ||||
---|---|---|---|---|
| ||||
Posted in reply to mipri | 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"));
|
December 30, 2019 Re: Concatenation/joining strings together in a more readable way | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | 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 {}. |
December 31, 2019 Re: Concatenation/joining strings together in a more readable way | ||||
---|---|---|---|---|
| ||||
Posted in reply to mipri | 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 {}.
Can you get me a bether? Like writeln("Hello #{var}");
|
December 31, 2019 Re: Concatenation/joining strings together in a more readable way | ||||
---|---|---|---|---|
| ||||
Posted in reply to mipri | 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..$]);}
|
January 02, 2020 Re: Concatenation/joining strings together in a more readable way | ||||
---|---|---|---|---|
| ||||
Posted in reply to BoQsc | On Wednesday, 25 December 2019 at 12:39:08 UTC, BoQsc wrote: > Are there any other ways to join two strings without Tilde ~ character? > I can't seems to find anything about Tilde character concatenation easily, nor the alternatives to it. Can someone share some knowledge on this or at least point out useful links/resources? import std; import std: Format = format; // Function format() string format(T...)(string text, T args){ foreach(n, i; args){ text = text.replace("{%d}".Format(n+1), "%s$s".Format("%" ~ to!string(n+1))); } return text.replace("{}", "%s").Format(args); } void main(){ writeln("Hi {} how are you {}?".format("Marcone", "today")); // Hi Marcone how are you today? writeln("My name is {2} and I live in {1}.".format("Brazil", "Marcone")); // My name is Marcone and I live in Brazil. writeln("We are {2} and {1}. I am {} and you {}. ".format("Marcone", "Paul")); // We are Paul and Marcone. I am Marcone and you Marcone. } |
Copyright © 1999-2021 by the D Language Foundation