String to char*

Hey

How would I convert a String into either a std::String or char*? The reason being I have another library that cannot understand Juce Strings.

Thanks.

…just cast it to a const char*. There are plenty of comments about this in the docs.

const char* cstr = (const char*)JUCEString;

Aye.

That’n’all.

Oh, its that simple…
I have allways done JUCEString->toUTF8() :slight_smile:

EDIT: But when thinking about it, “->toUTF8()” is actually shorter than “(const char*)”…

Oh, its that simple…
I have allways done JUCEString->toUTF8() :slight_smile:

EDIT: But when thinking about it, “->toUTF8()” is actually shorter than “(const char*)”…[/quote]

Yeah, but I don’t know what the fuck UTF8 is!

Oh, its that simple…
I have allways done JUCEString->toUTF8() :slight_smile:

EDIT: But when thinking about it, “->toUTF8()” is actually shorter than “(const char*)”…[/quote]

Yeah, but I don’t know what the fuck UTF8 is![/quote]

Ignorant idiot.

that’s a bit harsh!

Sorry.

I just said to Jade just now, “What the fucks UTF8?”, she didn’t know either!

I propose we change it’s name to WTF8, so as to clear up any confusion.

I just said to Jade just now, “What the fucks UTF8?”, she didn’t know either!

I propose we change it’s name to WTF8, so as to clear up any confusion.[/quote]

Excellent idea!

c’mon, guys ! print this article to read it next time you wait for your application to compile and link.

Jules mate, I think our sense of humour is wasted on these people.

Didn’t mean to be too serious, just my 2 cents…
actualy I like the wtf idea a lot, I think juce would benefit from being less clean and well written, to give it some more personnality.
grep -r ‘wtf’ juce/src/* gives nothing and that’s too bad…

[quote=“thomas”]Didn’t mean to be too serious, just my 2 cents…
actualy I like the wtf idea a lot, I think juce would benefit from being less clean and well written, to give it some more personnality.
grep -r ‘wtf’ juce/src/* gives nothing and that’s too bad…[/quote]

lol. aye! we could have the juce equivalent of Javas “Object” or COMs “IUnknown” (I think) where all Juce classes are descendants of class JuceWTF!

well it tickled me.

Is this const char* cast still supposed to be working? It gives me a compiler error.
Also, I’m a little confused about the documentation of toUtf8(). In the doc, it says, it’ll return a reference to the string’s internal data. However, when stepping through the code, it creates a copy of the internal data, and then the copy gets lost when the function returns.

Casting a string to a const char* would be meaningless because it wouldn’t specify what format the 8-bit data should take - that’s why you should call toUTF8 or toCString instead.

Don’t worry about how it works, the only thing you need to be aware of is that the pointer may become invalid when you call another method on the string object.

yea, that’s why I was puzzled that so many people (including you) said it would work.

my problem is that I’m trying to return a pointer of what toUtf8() returned from an AudioPluginInstance object.
So, my code looks something like this

const char *MyClass::GetPluginName()
{
return audioPluginInstanceObject->getName().toUtf8();
}

this doesn’t work for some reason (the returned pointer shows undefined data). I tried using toCString but got the same result.

Well of course that won’t work! :roll:

When I said “the pointer that is returned becomes invalid as soon as you call another method on the String object”, that obviously also includes calling its destructor!

right…
can you think of solution to my problem? I basically just want to get a reference to the String’s internal data, that I can pass through functions as a const char *, kind of the way string::c_str() works.