digitalmars.D.learn - Concatenation/joining strings together in a more readable way
- BoQsc (6/6) Dec 25 2019 Are there any other ways to join two strings without Tilde ~
- Tobias Pankrath (5/11) Dec 25 2019 Tilde operator is documented under cat expression:
- mipri (19/30) Dec 25 2019 Huh?
- Marcone (20/50) Dec 29 2019 Use Python format() style:
- mipri (4/17) Dec 30 2019 This leaks too much.
- Marcone (18/24) Jan 01 2020 import std;
- Marcone (20/26) Jan 02 2020 Fixed last bug, now is working fine:
- bachmeier (4/10) Jan 02 2020 There is a string interpolation DIP in progress. Doesn't help you
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?
Dec 25 2019
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
Dec 25 2019
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_expressionsIs ~ 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
Dec 25 2019
On Wednesday, 25 December 2019 at 13:07:44 UTC, mipri wrote:On Wednesday, 25 December 2019 at 12:39:08 UTC, BoQsc 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; } void main() { string name = "Marcone"; writeln("Helo {}".format(name)); // Helo Marcone writeln("Helo {0}".format(name)); // Helo Marcone }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_expressionsIs ~ 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
Dec 29 2019
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 {}
Dec 30 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:This function replace {} for arguments received. You just need don't send {} as arguments. I tested native function format() in Python: 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"));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 {}
Dec 30 2019
On Monday, 30 December 2019 at 10:23:14 UTC, Marcone wrote:On Monday, 30 December 2019 at 09:41:55 UTC, mipri wrote: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 {}.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: Nothing wrong, working same way.
Dec 30 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: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 {}.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: Nothing wrong, working same way.
Dec 31 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:Try this: import std: Format = format; string format(T...)(T text){return text[0].replace("{}", "%s").Format(text[1..$]);}On Monday, 30 December 2019 at 09:41:55 UTC, mipri wrote: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 {}.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: Nothing wrong, working same way.
Dec 31 2019
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. }
Jan 01 2020
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?Fixed last bug, now is working fine: import std; import std: Format = format; // Function format() text = text.Replace("%", "%%"); 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. }
Jan 02 2020
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?There is a string interpolation DIP in progress. Doesn't help you now, but a future reader might find this thread. https://github.com/dlang/DIPs/blob/master/DIPs/DIP1027.md
Jan 02 2020