1.Cocos2d-x中[NSString stringWithFormat:]替代方法
unsigned int n;
[NSString stringWithFormat:@"%u", n];
在Cocos2d-x中,可以利用下列方式
char* tempString = new char;
sprintf(tempString, "%u", n);
头文件
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
2:字符串的查找,替换
< 1 >
char str[64] = {0};
for(int i = 1; i <= 3; i++)
{
//输出到指定字符串
sprintf(str, "filed_%d.png", i);
CCLOG("--------str = %s", str);
// Cocos2d: --------str = filed_1.png
// Cocos2d: --------str = filed_2.png
// Cocos2d: --------str = filed_3.png
}
< 2 >
对于字符串的查找和替换,使用char*就过于繁琐了。而string类其实已经提供了这些基本的方法。比如我们在一个string中查找一个字符串并替换它:
string contentStr = string("http://www._3.com");
int index = contentStr.find("_3");
contentStr.replace(index, sizeof("_3") - 1, "baidu");
CCLOG("---------contentStr = %s", contentStr.c_str());
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交