Conversion between string and char* char[]

Source: Internet
Author: User

The conversion between string and char* char[], a friend of the need can refer to below.




1, it must be understood first that string can be considered as a container of characters as an element. Character composition sequence (string). Sometimes traversal in a sequence of characters, the standard string class provides an STL container interface. With some member functions such as Begin (), End (), iterators can be positioned according to them.



Note that unlike char*, a string does not necessarily end with null (' s '). The string length can be based on length (), and string can be accessed from the subscript. Therefore, you cannot assign a string directly to char*.



2. Convert String to char *



If you want the string to be converted directly to the const char * type. There are 2 functions that can be used with a string.



One is. C_STR () and one is the data member function.



Examples are as follows:



string S1 = "Abcdeg";

const char *k = S1.C_STR ();

const char *t = S1.data ();

printf ("%s%s", k,t);

cout<<k<<t<<endl;



As above, can be output. Content is the same. But can only be converted to const char*, if you remove the const compilation cannot pass.



Then, if you want to convert to char*, you can use a member function copy of string to implement it.



string S1 = "ABCDEFG";

Char *data;

int len = S1.length ();

data = (char *) malloc ((len+1) *sizeof (char));

S1.copy (data,len,0);

printf ("%s", data);

cout<<data;



3. char * converted to String



You can assign values directly.



string S;



Char *p = "ADGHRTYH";



s = p;



But this is going to be a problem.



There is one situation I would like to explain. After we have defined a string type, use printf ("%s", S1) and the output will be problematic. This is because "%s" requires the first address of the object that follows. But string is not such a type. So it must be wrong.



With the cout output is no problem, if you must be printf output. Then you can do this:

printf ("%s", S1.c_str ())





4, char[] convert to String



This can also be directly assigned to a value. But the problem will also arise. Need the same treatment.





5. Convert string to char[]



This is because we know the length of the string, can be obtained according to length () function, and can be directly accessed according to subscript, so we can assign a value with a loop.



Such conversions are not directly assignable.



String pp = "Dagah";

Char P[8];

int i;

For (I=0;i<pp.length (); i++)

P[i] = Pp[i];

P[i] = ' + ';

printf ("%s\n", p);

cout<<p;

Conversion between string and char* char[]

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.