C++文本处理——字符串语法

C++字符串语法学习

String 类

1.0 源

STLbasic_string类实例化

1.1 构造方法

string s1; 				// s1 = ""
string s2("Hello"); 	// s2 = "Hello"
string s3(4,'K'); 		// s3 = "KKKK"
string s4("12345",1,3); // s4 = "234" 
string s5="abcde"; 		//s5 = "abcde" 

1.2 赋值方法

直接赋值方法支持char*指针类(常量或变量)、char类(常量或变量)

string s;
s = "12345";       // 直接赋值常量char*类值
char* c = "abcd";  
s = c;			   // 直接赋值变量char*类值
s = 'q'; 		   // 直接赋值变量char类值变量(常量同理)

assign对象函数,用于赋值。

string s,t="aaag";
s.assign(t);			// s = t
s.assign("Hello");      // s = "Hello"
s.assign(4,'K'); 		// s = "KKKK"	
s.assign("12345",1,3);	// s = "234"

1.3 求字符串长度

sizelength都可以直接求出字符串长度

string s = "abcdef"  // s.length() = s.size() = 6

注意:.size().length()的类型是unsigned int可能发生错误。

下面是错误的写法:

string str="hello world";
if (-1 < str.length())
    cout << "true";
else
    cout << "false";

下面是正确的写法:

string str="hello world";
if (-1 < (int) str.length())
    cout << "true";
else
    cout << "false";

原因:强制类型转换将int类型的-1自然溢出为一个unsigned int类型内较大的数。

1.4 字符串的连接

  • 方法一:直接使用+/+=连接两个完整的字符串

  • 方法二:使用成员函数append,如:

    string a="123",b="abc";
    a.append(b); // a = "123abc"
    a.append(b,1,2); // a = "123abcbc"
    a.append(3,'K'); // a = "123abcbcKKK"
    a.append("ABCDE",2,3);// a = 123abcbcKKKCDE
    

1.5 字符串的比较

  • 方法一:使用</>/<=/>=/==等符号按照字典序比较

  • 方法二:使用compare成员函数

    • 返回负数值当前字符串较小
    • 返回0和当前字符串完全相等
    • 返回正数值另外一个字符串较小
    string a="123",b="abc";
    a.compare(1,2,b,1,2);    // a字符串从下标为1开始的2个字符构成的子串和b字符串从下标为1开始的2个字符串构成的子串比较
    a.compare(b,1,2);		 // a字符串和b字符串从下标为1开始的2个字符串构成的子串比较
    a.compare(1,2,b);		 // a字符串从下标为1开始的2个字符构成的子串和b字符串比较
    a.compare(b);			 // a字符串和b字符串比较
    

1.6 求字符串的子串

使用substr函数

a.substr(2,3); 				// 求a字符串从下标为2开始的3个字符构成的子串
a.substr(2);				// 求a字符串从小标为2开始到末尾的子串

1.7 交换字符串

使用swap函数

string a="123",b="abc";
a.swap(b);
swap(a,b);
// 上述两条语句都实现了a字符串和b字符串的交换

1.8 字符串操作

1.8.1 查找

  • find从前到后查找第1次出现的字符或者子串位置。
  • rfind从后到前查找第1次出现的字符或者子串位置。
  • find_first_of 从前往后查找第1次出现某字符串中包含的任一字母出现的位置。
  • find_last_of 从后往前查找第1次出现某字符串中包含的任一字母出现的位置。
  • find_first_not_of 从前往后查找第1次出现某字符串中未包含的任一字母出现的位置。
  • find_first_not_of 从后往前查找第1次出现某字符串中未包含的任一字母出现的位置。

若未找到,返回-1

string a="aabbcdefghijk";
printf("%d\n",a.find('a'));					// 输出 0
printf("%d\n",a.find("abb"));				// 输出 1
printf("%d\n",a.find_first_of("bcd"));		// 输出 2
printf("%d\n",a.find_last_of("bcd"));		// 输出 5
printf("%d\n",a.find_first_not_of("abc"));	// 输出 5
printf("%d\n",a.find_last_not_of("abc"));	// 输出 12

1.8.2 替换

使用replace函数

a.replace(1,2,"abcde",1,3);		// 将字符串a下标为1开始2个字符构成的子串替换为“abcde”第1个字符开始3个字符构成的子串
a.replace(1,2,"abcde");			// 将字符串a下标为1开始2个字符构成的子串替换为“abcde”
a.replace(1,2,4,'K');			// 将字符串a下标为1开始2个字符构成的子串替换为“KKKK”

1.8.3 删除

使用erase函数

a.erase(2,3);			// 删除字符串a下标为2开始的3个字符构成的子串
a.erase(3);				// 删除字符串a下标为3开始到字符串末尾构成的子串

1.8.4 插入

使用insert函数

a.insert(i,t);			//在字符串a下标为i处插入字符串t,i位置及其以后的原来值向后移动

1.9 输入输出

  • cin/cout : 默认遇到空格回车制表符等空白字符即字符串输入结束。

  • gets转化:可以输入含有空格键的数据:

    char str[105];	
    gets(str);
    string a=str; 	
    cout<<str;
    

1.10 STL相关

1.10.1 遍历

string s="0123456";
for (string::iterator p=s.begin();p!=s.end();++p) {
    putchar(*p);
}

1.10.2 排列和打乱

string s="0123456";
random_shuffle(s.begin(),s.end());		// 将字符串中所有元素随机打乱
next_permutation(s.begin(),s.end());	// 求当前字符串字典序下一个排列

字符数组

1.0 源

字符数组常量是一个指向一个字符串的指针或者固定的字符数组。

const char *s = "abcde";
const char t[] = "abcde";

字符数组变量一般指一个可以发生改变的字符数组。

char t[]="abcde";

字符数组由字符串+结尾符\0组成,若出现多个结尾符则以第一个出现的结尾符为准。

1.1 输入和输出

最优做法,利用字符数组进行自适应进行输入输出。

char t[]="";
cin>>t;
printf("%s\n",t);

1.2 求字符串的长度

使用函数strlen(s),但注意时间复杂度为\(O(n)\),应在循环外求出字符串的长度,再进行遍历。

char t[]="123";
int len=strlen(t);
printf("%d\n",len);

1.3 字符串的连接

使用函数strcat(s,t),将字符串t连接到字符串s后面,将s = s + t,而t的值不变。

char s[]="123",t[]="abc";
strcat(s,t);
printf("%s\n",s);
printf("%s\n",t);

1.4 字符串的复制

使用函数strcpy(s,t),将字符串t的内容复制到字符串s中。

char s[]="123",t[]="abc";
strcpy(s,t);
printf("%s\n",s);
printf("%s\n",t);

1.5 字符串比较

使用函数strcmp(s,t),若s=t返回0,若s>t返回正数,若s<t返回负数。

char s[105],t[105];
strcpy(s,"123"); strcpy(t,"abc");
int a = strcmp(s,t);
strcpy(s,"abc"); strcpy(t,"abc");
int b = strcmp(s,t);
strcpy(s,"abc"); strcpy(t,"123");
int c = strcmp(s,t);
printf("%d\n%d\n%d\n",a,b,c);

1.6 字符串交换

使用swap(a,b)可以将两个字符数组交换。

char s[105],t[105];
strcpy(s,"123"); strcpy(t,"a123bc");
swap(s,t);

posted @ 2021-08-09 13:15  Maystern  阅读(137)  评论(0编辑  收藏  举报