C++ string_demo
2014-04-28 20:03
246 查看
string类的使用 以及string与char * 之间相互转换
string字符串 长度 相加 比较 子串查找
#include <string> // 使用 string 类时须包含这个文件
#include <iostream>
using namespace std;
int main()
{
string str1;
// 输入与输出
cout << "输入字符串 str1" << endl;
cin >> str1;
getchar();
cout << str1 << endl;
// 一行行读取
cout << "输入字符串 str1" << endl;
getline( cin, str1 );//读入一行字符串string
cout << str1 << endl;
// 与 c字符转换
string str2("Hello World!"), str3;
char str4[50];
cout << "输入 C 字符串" << endl;
scanf("%s",str4);
str3= str4;//string str3; char str4[50] str3 = str4; (char[] -->string )
cout << "str2 is " << str2 << endl;
cout << "str3 is " << str3 << endl << endl << endl;
// 求string类型字符串的长度
string str5;
cout << "输入字符串 str5" << endl;
cin >> str5;
int len= str5.size();//string str5 str5.size()
cout << "字符串 str5的长度为" << len << endl << endl << endl;
// 遍历字符串例子
string str6;
cout << "输入字符串 str6" << endl;
cin >> str6;
int i;
for( i= 0; i< str6.size(); ++i )
cout << str6[i];
cout << endl << endl;
// 比较两个字符串 string 比较(直接 大小于)规则同 c字符串比较规则
string str7, str8;
cout << "输入字符串 str7, str8 , 中间用空格格开" << endl;
cin >> str7 >> str8;
if( str7< str8 )
cout << str7 << " 小于 " << str8 << endl;
else if( str7> str8 )
cout << str7 << " 大于 " << str8 << endl;
else
cout << str7 << " 等于 " << str8 << endl;
// 字符串**与字符相加***
string str9= "Darren";//可以改变的字符串 例如 +操作
char ch1= 'a', ch2= 'b';
str9= str9+ ch1;
cout << str9 << endl << endl;
str9= ch2+ str9;
cout << str9 << endl << endl << endl;
// 字符串***与字符串相加***
string str10= "Acm", str11= "ICPC";
// str10.append( str11 );//append
str10 = str10 + str11;//效果同上
cout << str10 << endl << endl;
// 字符串是否包含子串 如果包含 则返回子串在目标串中第一次出现的位置
string str12= "I am a student", str13= "student", str14= "aaaaaaa";
if( str12.find( str13 )!= -1 )
cout << "Find " << str13 << endl;
if( str12.find( str14 )== -1 )
cout << "Not Find " << str14 << endl;
// 转换成 c_字符串 (string -->char [])
string str15= "Hello World";
char str16[10];
strcpy(str16,str15.c_str());//char *strcpy(char* dest, const char *src);
printf("%s\n", str16 );
system("pause");
return 0;
}
string字符串 长度 相加 比较 子串查找
#include <string> // 使用 string 类时须包含这个文件
#include <iostream>
using namespace std;
int main()
{
string str1;
// 输入与输出
cout << "输入字符串 str1" << endl;
cin >> str1;
getchar();
cout << str1 << endl;
// 一行行读取
cout << "输入字符串 str1" << endl;
getline( cin, str1 );//读入一行字符串string
cout << str1 << endl;
// 与 c字符转换
string str2("Hello World!"), str3;
char str4[50];
cout << "输入 C 字符串" << endl;
scanf("%s",str4);
str3= str4;//string str3; char str4[50] str3 = str4; (char[] -->string )
cout << "str2 is " << str2 << endl;
cout << "str3 is " << str3 << endl << endl << endl;
// 求string类型字符串的长度
string str5;
cout << "输入字符串 str5" << endl;
cin >> str5;
int len= str5.size();//string str5 str5.size()
cout << "字符串 str5的长度为" << len << endl << endl << endl;
// 遍历字符串例子
string str6;
cout << "输入字符串 str6" << endl;
cin >> str6;
int i;
for( i= 0; i< str6.size(); ++i )
cout << str6[i];
cout << endl << endl;
// 比较两个字符串 string 比较(直接 大小于)规则同 c字符串比较规则
string str7, str8;
cout << "输入字符串 str7, str8 , 中间用空格格开" << endl;
cin >> str7 >> str8;
if( str7< str8 )
cout << str7 << " 小于 " << str8 << endl;
else if( str7> str8 )
cout << str7 << " 大于 " << str8 << endl;
else
cout << str7 << " 等于 " << str8 << endl;
// 字符串**与字符相加***
string str9= "Darren";//可以改变的字符串 例如 +操作
char ch1= 'a', ch2= 'b';
str9= str9+ ch1;
cout << str9 << endl << endl;
str9= ch2+ str9;
cout << str9 << endl << endl << endl;
// 字符串***与字符串相加***
string str10= "Acm", str11= "ICPC";
// str10.append( str11 );//append
str10 = str10 + str11;//效果同上
cout << str10 << endl << endl;
// 字符串是否包含子串 如果包含 则返回子串在目标串中第一次出现的位置
string str12= "I am a student", str13= "student", str14= "aaaaaaa";
if( str12.find( str13 )!= -1 )
cout << "Find " << str13 << endl;
if( str12.find( str14 )== -1 )
cout << "Not Find " << str14 << endl;
// 转换成 c_字符串 (string -->char [])
string str15= "Hello World";
char str16[10];
strcpy(str16,str15.c_str());//char *strcpy(char* dest, const char *src);
printf("%s\n", str16 );
system("pause");
return 0;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- 关于指针的一些事情
- String.intern
- Prototype源码浅析 String部分(二)
- C/C++数据对齐详细解析
- C++中引用的使用总结
- C与C++之间相互调用实例方法讲解
- C++中引用(&)的用法与应用实例分析
- 解析C++ 浮点数的格式化输出
- 深入分析C++中几个最不常用的关键字
- c++中inline的用法分析
- C++ Primer 第一部分基本语言
- 深入解析C++ Data Member内存布局
- 从汇编看c++中默认构造函数的使用分析
- 关于C++中的友元函数的一些总结
- C++的sstream标准库详细介绍
- 基于C++自动化编译工具的使用详解