C++ string表示可变长的字符序列
需要的头文件 #include <string>
而不是 #include <string.h>
#include <string.h> 是C的头文件
string 定义在命名空间std中
所以加 using std::string; //不加这句 定义的每个string对象都得写成 std::string str;
- 初始化方式
string str = "abcdef";
string str1("123");
string str2 = "123";
string str3 = str;
string str4 = (str);
string str5(3,'L'); //连续n个字符L组成
判断是否为空
empty()求实际大小
size()判断是否相等
==
!=累加
str = str1 + str2;
str= “hello” + “world”; //错误 必须保证 + 两侧至少一个是string类型取出字符串的值 -
c_str()访问string中的字符 下标和迭代器 下面是下标法
int i = 0;
int s_size = str.size();
for (i;i<s_size;i++)
{
printf("%c\n",str[i]);
}
- 处理字符串中的单个字符
isalnum(c) 如果 c 是字母或数字,则为 True。
isalpha(c) 如果 c 是字母,则为 true。
iscntrl(c) 如果 c 是控制字符,则为 true
isdigit(c) 如果 c 是数字,则为 true。
isgraph(c) 如果 c 不是空格,但可打印,则为 true。
islower(c) 如果 c 是小写字母,则为 true。
isprint(c) 如果 c 是可打印的字符,则为 true。【注意:可打印的字符是指那些可以表示的字符】
ispunct(c) 如果 c 是标点符号,则 true。【注意:标点符号则是除了数字、字母或(可打印的)空白字符(如空格)以外的其他可打印字符】
isspace(c)如果 c 是空白字符,则为 true。【注意:空白字符则是空格、制表符、垂直制表符、回车符、换行符和进纸符中的任意一种】
isupper(c) 如果 c 是大写字母,则 true。
isxdigit(c) 如果是 c 十六进制数,则为 true。
tolower(c) 如果 c 大写字母,返回其小写字母形式,否则直接返回 c。
toupper(c)如果 c 是小写字母,则返回其大写字母形式,否则直接返回 c。