字符串

字符串赋值(C字符串)

将字符串b赋值给字符串a: =:定义时赋值,b必须是字符串变量。

 strcpy(char *a, char *b) :b须是字符串常量或C字符串。

例:

char a[20], b[20] = "Hello";
strcpy(a, b);       //a = "Hello"; 
strcpy(a, "OK");    //a = "OK"

 

字符串赋值(string)

给string型变量赋值:=

 a=b :b可以是任意类型字符串(字符串常量、C字符串或string)。

例:

string a, b("Hello"); 
char c[10] = "World";
a = b;       //a = "Hello"
a = c;       //a = "World"
a = "OK";  //a = "OK"

 

字符串连接(C字符串)

将字符串b接到字符串a后面:

 strcat(char *a, char *b) :b 须是字符串常量或C字符串。

例:

char a[20] = "Hello", b[20]= ", world!";
strcat(a, b);     //"Hello, world!"
strcat(a, "OK");   //a = "Hello, world!OK"

 

字符串连接(string)

将字符串a和字符串b前后连接:+、+=

 a+b 或 a+=b :a和b须一个是string型,另一个任意类型,连接结果是string型。

 例:

string a(""), b("Hello"); 
char c[10] = ", World!";
string d = "Hello" + a + c;   //d = "Hello, World!"
a += b;     //a = "Hello"
a += c;     //a = "Hello, World!"
a += "OK";   //a = "Hello, World!OK"

 

字符串比大小(C字符串)

C字符串比较函数:strcmp()

 strcmp(char *a, char *b) :若a=b,返回0;若a>b,返回正整数;若a<b,返回负整数。

例:

char a[10] = "abc", b[10] = "abcd";
int n = strcmp(a, b);
if (n > 0) cout << ">";
else if (n < 0) cout << "<";
else cout << "=";

 

字符串比大小(string)

string字符串比较运算符:<、<=、==、>、>=、compare compare方法返回一个整数,参考strcmp()。

例:

string a("abc"), b("abcd");
if (a > b) cout << ">";
else if (a < b) cout << "<";
else cout << "=";    

string a("abc"), b("abcd");
int n = a.compare(b);
if (n > 0) cout << ">";
else if (n < 0) cout << "<";
else cout << "=";

 

查找字符(C字符串)

在C字符串中查找字符:strchr()

 strchr(char *a, char ch) :返回a[ ]中左起第一个ch的地址,若a[ ]中没有ch则返回NULL(0)。

例:

char a[] = "abcbd";
cout << (int)strchr(a, 'b') << endl;    //地址
cout << (int)strchr(a, 'e') << endl;    //0
cout << strchr(a, 'b') - a << endl;        //1

 

查找字符串(C字符串)

在C字符串中查找字符串:strstr()

 strstr(char *a, char *b) :返回a[ ]中左起第一个b[ ]的地址,若a[ ]中没有b[ ]则返回NULL(0)。

例:

char a[] = "abcbd", b[] = "bc", c[] = "dc";
cout << strstr(a, b) - a << endl;    //1
cout << strstr(a, c) - a << endl;    //-a的地址

 

查找字符或字符串(string)

在string字符串中查找字符或字符串:find。

find返回位置,是string::size_type(unsigned int)类型,若找不到返回string::npos (unsigned int最大值,表示无穷大)。

find() 有两种格式:

 a.find(char ch, n = 0) :在a中查找字符ch,返回a中左起第一个ch的下标。

 a.find(string b/char *b, n = 0) :在a中查找字符串b,返回a中左起第一个b中b第一个字符的下标。

rfind():从右往左查找,使用方法与find相同。

(n:从第n个元素开始查找,n缺省值为0,表示从头开始查找)

string a("abcdabce"), b("bc");
char c[10] = "bc", ch = 'd';
string::size_type pos;
    //在string中查找string字符串
pos = a.find(b);        //pos = 1
pos = a.find(b, 2);        //pos = 5
    //在string中查找char数组字符串
pos = a.find(c);        //pos = 1
pos = a.find(c, 2);        //pos = 5
    //在string中查找字符
pos = a.find(ch);        //pos = 3
pos = a.find(ch, 5);    //pos = string::npos
    //反向查找
pos = a.rfind(b);        //pos = 5
pos = a.rfind(b, 2);    //pos = 1

 

截取子串

string成员函数: string substr(int n, int m) 

n:子串起始位置,默认从0开始

m:子串长度,默认为string::npos(无穷大),表示到字符串结束。

substr只返回子串,不修改原字符串。

例:

string s1 = "this is ok";
string s2 = s1.substr(2, 4);  // s2 = "is i"
s2 = s1.substr(2);          // s2 = "is is ok"

 

删除子串

string成员函数: string erase(int n, int m) 

功能:从位置n开始,删除连续m个字符

n默认等于0,m默认为string::npos

函数直接修改原字符串,并返回修改后的字符串。

例:

string s1("Real Steel");
s1.erase(1, 3);      //s1 = "R Steel"
s1.erase(5);          //s1 = "R Ste"

 

插入字符串

string成员函数:insert()

 string insert(int n, string b/char *b)  :在位置n插入字符串b

 string insert(int n, int m, char ch) :在位置n插入m个字符ch

函数直接修改原字符串,并返回修改后的字符串。

例:

string s1("Limitless"), s2("00");
s1.insert(2, "123");     //s1="Li123mitless"
s1.insert(3, s2);         //s1="Li10023mitless"
s1.insert(3, 2, 'X');    //s1="Li1XX0023mitless"

 

替换子串

string成员函数:replace()

 string replace(int n, int m, string b/char* b)  :将子串(n,m)替换为b

 string replace(int n, int m, string b/char* b, int x, int y)  :将子串(n,m)替换为b的子串(x,y)

 string replace(int n, int m, int k, char ch) :将子串(n, m)替换为k个ch

函数直接修改原字符串,并返回修改后的字符串。

例:

string s1("Real Steel"), s2("4321");
s1.replace(1, 3, "1234");        //s1 = "R1234 Steel"
s1.replace(1, 3, s2);        //s1 = "R43214 Stell" 
s1.replace(2, 4, "abcd", 1, 2);    //s1 = "R4bc Stell" 
s1.replace(2, 2, s2, 1, 3);    //s1 = "R4321 Stell"
s1.replace(1, 4, 2, '0');        //s1 = "R00 Stell"

 

判断类型

是否为十进制数字:isdigit()

是否为英文字母:isalpha()

是否为大写字母:isupper()

是否为小写字母:islower()

 


 

呃,我好像只学会了这么多?

 

posted @ 2023-02-24 15:41  WBCAZ  阅读(48)  评论(0)    收藏  举报