- #include<stdio.h>
- #include<iostream>
- #include<assert.h>
- #include<stdlib.h>
- #include<string>
- using namespace std;
- /*
- 字符串处理函数
- strlen(str)// 字符串长度
- strcmp(str1,str2) 字符串比较 strncmp(str1,str2,n)// 比较前 n 个
- strcat(str1,str2) 字符串连接 strncat
- strcpy(str1,str2);strncpy(str1,str2,n); 字符串拷贝
- */
- /*// 按以下函数进行实际操作
- C++
- <string>
- string str;
- 字符串长度: len=str.length(); || len=str.size();
- 字符串比较: str.compare(str2); || str.compare(pos1,len1;str2,pos2,len2);> 返回 1 ,= 返回 0 , < 返回 - 1 pos: 字符串下标
- 附加: str1+=str2; || str1.append(str2); || str1.append(str2,pos2,le2);
- 字符串提取: str2=str1.substr(); || str2=str1.substr(pos1); || str2=str1.substr(pos1,len1);
- 自符串搜索: where=str1.find(str); || where =str1.find(str2,pos1); 搜索位置 || where=str1.rfind(str2); 从后搜
- 插入字符串: str1.insert(pos1,str2);|| str1.insert(pos1,str2,pos2,len2); || str1.insert(pos1,numchar,str2); 插入次数
- 替换字符串: str1.replace(pos1,str2); ||str1.replace(pos1,str2,pos2,len2);
- 删除字符串: str.erase(pos,len); || str.clear();
- 交换字符串: swap(str1,str2);
- C-->C++ :char*cstr="hello";string str1;cstr=cstr:string str2(cstr);
- */
- char *My_strcpy( char*str1,const char *str2)
- {
- assert((str1 != NULL) &&(str2 != NULL));
- char* to = str1;
- while ((*str1++ = *str2++) != '\0');
- return to;
- }
- // 结果最后附页
- int main()
- {
- string str1 ="hello word";
- string str2 = "hello";
- str1.erase(2, 3);
- cout << "str1" << str1 << endl;
- //str1.replace(0,str2);
- //str1.replace(2, str2, 2, 2);
- str1.insert(2, str2); //hehellollo Word
- cout << "str1" << str1 << endl;
- cout << str1.find('l') << endl; //2
- cout << str1.rfind('l') << endl;//3
- const char *str3 = "c hello";
- char *str4 = "c hello word";
- string str5(str4);
- cout << "str5 :" << str5 << endl;
- cout << "str3 :" << str3 << endl;
- cout << "str4 :" << str4 << endl;
- printf("%s\n", str3); //true :str3 为 C
- printf("%s\n", str4); //true :str4 为 C
- //printf("%s\n", str5);//error:str5 为 C++
- //cout<< My_strcpy(str4, str3) << endl;
- cout << "str1 字符串为"<<str1 << endl;
- printf("%d\n",str1.length()); //10
- printf("%d\n", str1.compare(str2)); //1
- printf("%d\n", str1.compare(2,1,str2,3,1)); // 0
- str1 + str2;//str1=hello wordhello;
- cout << "str1"<<str1<< endl;
- str1.append(str2);//str1=hello wordhello hello;
- cout <<"str1"<< str1 << endl;
- str1.append(str2, 2, 4);//str1=hello wordhello hello llo;
- cout << "str1" << str1 << endl;
- str2 = str1.substr();//str2=hello Word;
- cout << "str2" << str2 << endl;
- str2 = str1.substr(2);//str2=llo Word;
- cout <<"str2"<< str2 << endl;
- str2 = str1.substr(2, 4);//str2=llo
- cout << "str2"<<str2 << endl;
- swap(str1, str2);//2
- cout << "str1 :" << str1 << "str2 :" << str2 << endl;
- return 0;
- }
- /*
- str1 he Word
- str1 hehello Word
- 4
- 5
- str5 :c hello Word
- str3 :c hello
- str4 :c hello Word
- c hello
- c hello Word
- str1 字符串为 hehello Word
- 12
- -1
- -1
- str1 hehello Word
- str1 hehello wordhello
- str1 hehello wordhellollo
- str2 hehello wordhellollo
- str2 hello wordhellollo
- str2 hell
- str1 :hell str2 :hehello wordhellollo
- */
来源: http://www.bubuko.com/infodetail-3117916.html