Convert qstring, String, char *, and cstring

Source: Internet
Author: User

Recently, I am looking at QT development to summarize several methods for String Conversion.

There are many methods. Here is the most common method: char * is the most basic type. Other types can be converted to char * first, and then converted to the type.

Cstring-char *

Cstring CSTR = "This Is A cstring .";
Char * Ch = CSTR. getbuffer (CSTR. getlength () + 1 );
CSTR. releasebuffer ();

Tchar * tstr = _ T ("This Is A tchar * string .");

Cstring CSTR;
// Assign values directly

CSTR = tstr;
// Or use the format Function

CSTR. Format (_ T ("% s"), tstr );

 

String-char *

Const char * str1 = "this is a const char *.";
String STR (str1 );

String str1 = "this is a string .";
Char * str2 = const_cast <char *> (str1.c _ STR ());

 

Qstring-char *

Const char * STR = "this is a C string .";

Qstring qstr (qstring: fromlocal8bit (STR ));
Const char * str2 = qstr. tolocal8bit ();

 

Qstring-string

# Include <qstring>
# Include <vector>
# Include <iostream>
# Include <string>

Using namespace STD;
Qstring s2q (const string & S );
String q2s (const qstring & S );

Qstring s2q (const string & S)
{
Return qstring (qstring: fromlocal8bit (S. c_str ()));
}
String q2s (const qstring & S)
{
Return string (const char *) S. tolocal8bit ());
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.