How to add a character literal to a string without ~ operator?

IchorDev zxinsworld at gmail.com
Mon Apr 8 12:17:46 UTC 2024


On Thursday, 4 April 2024 at 18:14:54 UTC, BoQsc wrote:
> I'm looking for more readable standard function to add a 
> **character** literal to a **string**.

Concatenate is the verb you're looking for, not add. 'Adding' a 
`char` to a `string` sounds like you want `myString[] += 
myChar;`, which wouldn't compile because `string`s are aliases of 
`immutable(char)[]`.

> Pseudo example:
> ```
> import std;
> void main(){
> 	string word = hello;
> 	join(word, 'f', " ", "World");
> 	writeln(word);   	// output: hellof World
> 	
> }
> 	
> ```

I'd usually use 
[`text`](https://dlang.org/phobos/std_conv.html#text). It 
automatically converts each parameter to a string and 
concatenates all of them. If you prefer format strings, there's 
[`format`](https://dlang.org/phobos/std_format.html#format).


More information about the Digitalmars-d-learn mailing list