一、字符串字面值
字符串字面值是一串常量字符,字符串字面值常量用双引号括起来的零个或多个字符表示,为兼容C语言,C++中所有的字符串字面值都由编译器自动在末尾添加一个空字符。
字符串没有变量名字,自身表示自身
字符串字面值的连接:
多行字面值:
=================================================
1. string literal:字符串直接量:
2. 字符串直接量可以赋值给变量,但是与字符串直接量相关联的内存空间位于只读部分,因此它是常量字符数组。
1. 支持<cstring>中许多函数完成的同样操作。
2. 字符串定义:
2.1 C风格字符串的使用
C++语言通过(const) char *类型的指针来操纵C风格字符串。
//操纵C风格字符串的标准库函数(参数类型省略,都是char *类型):
strlen(s) // 返回s的长度,不包括字符串结束符null
strcmp(s1, s2) //当s1<s2时,返回值<0 ,当s1=s2时,返回值=0 ,当s1>s2时,返回值>0
strcat(s1, s2) // 将字符串s2连接到s1后,并返回s1
strcpy(s1, s2) // 将s2复制给s1,并返回s1
strncat(s1, s2, n) // 将s2的前n个字符连接到s1后面,并返回s1
strncpy(s1, s2, n) // 将s2的前n个字符复制给s1,并返回s1
if(cp1 < cp2) // compares address, not the values pointed to
const char *cp1 = "A string example";
const char *cp2 = "A different string";
int i=strcmp(cp1, cp2); // i is positive
i=strcmp(cp2, cp1); // i is negative
i=strcmp(cp1, cp1); // i is zero
</div>
2.3 永远不要忘记字符串结束符null