View previous topic :: View next topic |
Author |
Message |
Hero Doug
Joined: 17 Aug 2008 Posts: 4
|
Posted: Sun Mar 22, 2009 2:33 am Post subject: const(char)[] to string conversion |
|
|
I'd like to know how I go about converting/copying the contents of const(char)[] to a string.
I have an associative array that's being looped though and on each loop I want to take the key and use it in a string replacement to swap out a tag in a string.
Here is what I'd like to do:
Code: |
foreach (const(char)[] key, string value; this.pairs) {
writeln(key);
writeln(value);
writeln(replace(this.tpl, key, value));
}
|
It throws the error
Quote: |
SwapElements.d(36): Error: cannot implicitly convert expression (key) of type const(char)[] to invariant(char)[]
|
According to the manul replace requires a string, so I'd like to convert const(char)[] to a string so I can use this function.
I'll even accepts an ugly solution like looping through the individual characters and appending them to a string as it'll make swapping out tags more elegant.
Thanks. |
|
Back to top |
|
|
Hero Doug
Joined: 17 Aug 2008 Posts: 4
|
Posted: Sun Mar 22, 2009 3:30 am Post subject: |
|
|
SOlution:
I changed string[char[]] pairs; to string[string] pairs; I can now use the key value directly. |
|
Back to top |
|
|
csauls
Joined: 27 Mar 2004 Posts: 278
|
Posted: Sun Mar 22, 2009 6:44 pm Post subject: |
|
|
You could also use the property to create an invariant duplicate. For your case, though, I think you've found the "right way." _________________ Chris Nicholson-Sauls |
|
Back to top |
|
|
Hero Doug
Joined: 17 Aug 2008 Posts: 4
|
Posted: Sun Mar 22, 2009 8:49 pm Post subject: |
|
|
Thanks for the reply, I'll keep that in mind if I run into a similar situation. |
|
Back to top |
|
|
|