1.
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
void main()
{
string test;
char szBuffer[1024] = {0};
char szBuffer02[1024] = {0};
int a=1, b=2,c=3;
sprintf(szBuffer,"%d,%d,%d",a,b,c );
sprintf(szBuffer02,"%d,%d",a,b );
test += szBuffer;
test += ",";
test += szBuffer02;
cout<<szBuffer<<endl;
cout<<szBuffer02<<endl;
for(int i=0;i<strlen(test.c_str());i++)
{
cout<<test[i];
}
getchar();
}
2.
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
void PutInt(string &src,int data)
{
static bool once=false;
char szBuffer[1024] = {0};
sprintf(szBuffer,"%d",data );
if(once)
src += ",";
src += szBuffer;
once=true;
}
void main()
{
string test;
for(int i=1;i<10;i++)
{
PutInt(test,i);
}
for(int i=0;i<strlen(test.c_str());i++)
{
cout<<test[i];
}
getchar();
}
3.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
istringstream iss("12 34");
int a, b;
iss >> a >> b; //从字符串输入流iss中读取两个数
cout << a << "," << b << endl;
string str;
ostringstream oss(str); //输出到string str中
oss << a << " " << b;
cout << oss.str() << endl; //取出输出缓冲区的
getchar();
return 0;
}
4.replace
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string result;
string s = "AAAAAAAA";
char ch = 'C';
result = s.replace ( 1 , 3 , 4 , ch ); // s= "ACCCCAAAA
for (int i = 0;i<result.size();++i)
{
cout<<result[i];
}
cout<<endl;
system("pause");
return 0;
}
5.substr
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string s, ss;
s = "The rain in Spain falls mainly in the plain.";
ss = s.substr(12, 5); // Spain
for(int i=0;i<ss.size();++i)
cout<<ss[i];
cout<<endl;
ss = s.substr(12); // Spain falls mainly in the plain.
for(int i=0;i<ss.size();++i)
cout<<ss[i];
system("pause");
return 0;
}
6.判断字符是中文还是英文
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
unsigned char input[50] = "fdasfasffff中文";
int flag = 0;
for (int i = 0; i < 50; i++)
{
if (input[i] > 0xa0 && input[i] != 0)
{
if (flag == 1)
{
cout << "chinese character" << endl;
flag = 0;
}
else
{
flag++;
}
}
else if (input[i] == 0)
{
break;
}
else
{
cout << "english character" << endl;
}
}
system("pause");
return 0;
}