www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Concatenation/joining strings together in a more readable way

reply BoQsc <vaidas.boqsc gmail.com> writes:
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
next sibling parent Tobias Pankrath <tobias pankrath.net> writes:
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
prev sibling next sibling parent reply mipri <mipri minimaltype.com> writes:
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
Dec 25 2019
parent reply Marcone <marcone email.com> writes:
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 }
Dec 29 2019
parent reply mipri <mipri minimaltype.com> writes:
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
parent reply Marcone <marcone email.com> writes:
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: 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"));
Dec 30 2019
parent reply mipri <mipri minimaltype.com> writes:
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: 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 {}.
Dec 30 2019
next sibling parent Marcone <marcone email.com> writes:
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: 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 {}.
Dec 31 2019
prev sibling parent Marcone <marcone email.com> writes:
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: 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..$]);}
Dec 31 2019
prev sibling next sibling parent Marcone <marcone email.com> writes:
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
prev sibling next sibling parent Marcone <marcone email.com> writes:
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
prev sibling parent bachmeier <no spam.net> writes:
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