对于C和C++字符及字符串我以前接触的少,很多概念方法等并不是很明晰,在此做个笔记梳理下相关内容。
对于计算机来说本身并不存在字符(char
)这种东西,计算机存储的只是一个个的值,但是单纯的值对于我们来说并不适合表达阅读信息,所以人为的将值和字母符号等联系起来,常见的比如ASCII码。ASCII码将0~127的值对应为英语环境下的字母符号等,见下图:
http://www.asciima.com/
字符基本声明与使用可以看下图,注意单个字符用单引号''
包围:
根据上面说明和演示可以知道单个的char
其实就是将值换了个形式表达,所以单个的char
之间是可以进行大小比较以及加减乘除等各类运算的,其本质还是数值间运算。
在C语言中的字符串是以结束符\0
为结尾的字符数组,可以用下面方式声明:
char s1[6] = {'a','b','c','d','e','\0'};
char s2[6] = "ABCDE"; //同上面的方法,该方式声明时会在结尾自动补上'\0',所以长度也为6
char *s3 = "12345"; //理论上同char s3[6] = "12345";但实际使用时根据环境不同“12345”有可能会是无法修改的只读数据,所以常见的还会在前面加上const显性表示只读数据用
//同样的字符串以'\0'结尾,所以s3长度也为6;
char s4[6] = {'1', '2', '\0', '4', '5', '\0'}; //这里中间出现了'\0',所以当字符串使用时表示"12"
char s5[] = {'a','b','c','d','e','\0'}; //同s1
char s6[] = "ABCDE"; //同s2
字符串基本使用见下图:
注意:字符串是一种字符数组,但字符数组并不一定是字符串,关键在于有没有’\0’。
字符串相关的函数非常多,这里只列举些常用的,注意有些函数需要包含头文件string.h
、ctype.h
、stdlib.h
。
size_t strlen(const char *str)
\0
)char *strcpy(char *dest, const char *src)
char *strncpy(char *dest, const char *src, size_t n)
char *strcat(char *dest, const char *src)
\0
;char *strncat(char *dest, const char *src, size_t n)
\0
;char *strupr(char *s)
char *strlwr(char *s)
int tolower ( int c )
int toupper ( int c )
char *strchr(const char *str, int c)
\0
);char *strrchr(const char *str, int c)
\0
);char *strstr(const char *haystack, const char *needle)
\0
);int strcmp(const char *str1, const char *str2)
int strncmp(const char *str1, const char *str2, size_t n)
int isalpha ( int c )
int isdigit ( int c )
int isxdigit ( int c )
int islower ( int c )
int isupper ( int c )
char *itoa( int value, char *string,int radix)
char *ltoa( int value, char *string,int radix)
int atoi(const char *str)
long int atol(const char *str)
C++向下兼容C语言,所以可以按C语言来使用操作字符串(C语言中string.h
库在C++里推荐使用cstring
代替),另外C++ namespace std
中有string
类,可以使用string
对象。string
对象的加入使操作字符串变得方便许多,可以直接进行赋值与加法操作或是比较操作,使用示例见下面代码:
#include
#include //引入头文件
using namespace std;
int main()
{
string str1; //声明了一个空字符串
str1 = "12345"; //赋值
cout << "str1: " << str1 << endl; //打印输出
string str2("abcde"); //声明一个字符串
cout << "str2: " << str2 << endl; //打印输出
string str3("abcde", 3); //声明一个从输入字符串中从头开始取3个字符的字符串
cout << "str3: " << str3 << endl; //打印输出
string str4(str1, 1, 2); //声明一个从输入字符串中下标1开始两个字符的字符串
cout << "str4: " << str4 << endl; //打印输出
string str5(5, 'E'); //声明一个由5个E组成的字符串
cout << "str5: " << str5 << endl; //打印输出
cout << "str1 + str2: " << str1 + str2 << endl; //string加法
if(str1 == "12345") //比较
{
cout << "str1 == \"12345\": " << "true" << endl;
}
if(str1 > str5) //比较
{
cout << "str1 > str5: " << "true" << endl;
}
else
{
cout << "str1 > str5: " << "false" << endl;
}
//字符串的比较是从左到右依次进行,直到得出结果
return 0;
}
C语言字符串转为C++ string:
const char *ch = "hello"; //或者 char ch[] = "hello";
string str1(ch); //转换方法1
string str2 = ch; //转换方法2
C++ string转为C语言字符串:
string str("hello");
const char *ch = str.c_str(); //string转c语言字符串
字符串相关的方法非常多,这里只列举些常用的(很多方法都有非常多的重载,这里不一一列出),注意有些方法需要用到string
库。
int size()const
int length()const
;bool empty()const
void resize(int len,char c)
int stoi (const string& str, size_t* idx = 0, int base = 10)
long stol (const string& str, size_t* idx = 0, int base = 10)
to_string()
append()
insert()
erase()
replace()
find()
rfind()
compare()
字符串在上位机开发中是经常会用到的,了解相关内容还是蛮有必要的。
更多内容参考如下:
http://c.biancheng.net/view/1832.html
http://c.biancheng.net/view/1834.html
http://www.cplusplus.com/reference/cstring/
http://c.biancheng.net/view/2236.html
http://c.biancheng.net/view/400.html
http://www.cplusplus.com/reference/string/