Concatenation/joining strings together in a more readable way

Marcone marcone at email.com
Thu Jan 2 07:12:46 UTC 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?

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.
}


More information about the Digitalmars-d-learn mailing list