Strings in the Cocos2d-x:
<1>: Using const char* and std::string
Const char* is a C-style string, std::string is a C + + style string that encapsulates the const char *
Initialize the Std::string object:
std::string name = "Tony";
std:: string name = std::string ("micheal");
std::string pointer type:
std::string NAMEP = new std::string ("Micheal");
... ....
Delete NAMEP;
Convert the std::string type to the const char* type:
Const char* CString = Name.c_str ();
Const char* CString = Namep->c_str ();
<2>: Using cocos2d::__string
From the nsstring of Objective-c
The main static functions for creating it are as follows:
_static __string * Create (const std::string &STR)
_static__string* Createwitnformat (const char* format,......)
Conversions between data types:
Examples of cocos2d::__string to const char* types are as follows:
__string * name = __string::create ("Hi micheal");
const char * cstring = name->getcstring ();
The const char* is converted to the cocos2d::__string type, for example:
const char * cstring = "Hi micheal";
__string* ns = __string::createwithformat ("%s", CString);
Std::string converted to cocos2d::__string () type, for example:
std::string string = "Hi micheal";
__string * ns = __string::createwithformat ("%s", String.c_str ());
cocos2d::__string Convert to int type
int num = 123;
__string * ns = __stringcretaewitjformat ("%d", num);
int num2 = Ns->intvalue ();
When doing data conversion, it is best to include the head file
- #include <iostream>
- #include <cmath>
- #include <string>
- #include <sstream>
- USING_NS_CC;
- using namespace Std;
In Cocos2d-x, there is also a format brush: __string (data conversion often find her to do the middleman), then we want to convert the type, you can first brush the starting data type __string and then turn to the destination data type, this method is more convenient and practical.
int turn ccstringint num=5; ccstring* Ns=ccstring::createwithformat ("%d", num); ccstring to Intint px = Ns->intvalue ()//convert ccstring to int, to float type has floatvalue ()//=========================== ===========//ccstring turn Charconst char *string= ns->getcstring (); Char to Ccstringcstring.format ("%s", char *)//char CString ccstring* Ns=ccstring::createwithformat ("%s", char *);//== ====================================//string Turn Charconst char *string=std::string ("string"). C_str (); Char to stringstring S (char *);//======================================//string turn intstd::string s ("1234"); int n = std::atoi (s . C_STR ());//function prototype int atoi (const char *); int turn stringint a=3; ccstring* Ns=ccstring::createwithformat ("%d", a); string s=ns->m_sstring;//==================================== ==//string turn ccstringstd::string str = "123"; ccstring* Ns=ccstring::createwithformat ("%s", Str.c_str ()); ccstring Turn stringstd::string s= ns->m_sstring;//ns is a ccstring* type//======================================//char to int char *buf = "1122"; int n = std::atoi (BUF);//int to Charint a=3; ccstring* Ns=ccstring::createwithformat ("%d", a); const char* s=ns->getcstring ();
"Reprint" Cocos2d-x type conversion (ccstring int string char UTF-8) and string explanation