FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

const(char)[] to string conversion

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
Hero Doug



Joined: 17 Aug 2008
Posts: 4

PostPosted: Sun Mar 22, 2009 2:33 am    Post subject: const(char)[] to string conversion Reply with quote

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
View user's profile Send private message
Hero Doug



Joined: 17 Aug 2008
Posts: 4

PostPosted: Sun Mar 22, 2009 3:30 am    Post subject: Reply with quote

SOlution:

I changed string[char[]] pairs; to string[string] pairs; I can now use the key value directly.
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Sun Mar 22, 2009 6:44 pm    Post subject: Reply with quote

You could also use the
Code:
.idup
property to create an invariant duplicate. For your case, though, I think you've found the "right way."
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
Hero Doug



Joined: 17 Aug 2008
Posts: 4

PostPosted: Sun Mar 22, 2009 8:49 pm    Post subject: Reply with quote

Thanks for the reply, I'll keep that in mind if I run into a similar situation.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group