C++字符串操作

来源:互联网 发布:男装淘宝店 编辑:程序博客网 时间:2024/06/18 14:33

1. strcat——字符串连接

复制代码
 1 #include <iostream> 2 using namespace std; 3  4 int main(){ 5  6     char str[15] = "abcd"; 7     char str1[] = "abc"; 8     strcat(str, str1); 9     cout<<str<<endl;10 11     system("pause");12     return 0;13 }
复制代码

※注意点,第一个字符串数组要足够大,否则会有越界问题。

2. strcpy——字符串拷贝

复制代码
 1 #include <iostream> 2 using namespace std; 3  4 int main(){ 5  6     char str[15] = "abcd"; 7     char str1[] = "abc"; 8     strcpy(str, str1); 9     cout<<str<<endl;10 11     system("pause");12     return 0;13 }
复制代码

※注意点,第一个字符串数组要足够大,否则会有越界问题。另外第二个参数可以不是数组,可以是字符。

3. strcmp——字符串比较函数

复制代码
 1 #include <iostream> 2 using namespace std; 3  4 int main(){ 5  6     char str[15] = "abcd"; 7     char str1[] = "abc"; 8     if(0 == strcmp(str, str1)){ 9         cout<<"Equal."<<endl;10     }else{11         cout<<"Unequal."<<endl;12     }13 14     system("pause");15     return 0;16 }
复制代码

※注意点,前者大,返回1;后者大,返回-1;相等,返回0。

4. strupr——小写转大写

复制代码
 1 #include <iostream> 2 using namespace std; 3  4 int main(){ 5  6     char str[15] = "abcdf"; 7     char str1[] = "abcde"; 8     strupr(str); 9     cout<<str<<endl;10 11     system("pause");12     return 0;13 }
复制代码

5. strlwr——大写转小写

复制代码
 1 #include <iostream> 2 using namespace std; 3  4 int main(){ 5  6     char str[15] = "AASdf"; 7     strlwr(str); 8     cout<<str<<endl; 9 10     system("pause");11     return 0;12 }
复制代码

 

6. strlen——获取字符串长度

复制代码
 1 #include <iostream> 2 using namespace std; 3  4 int main(){ 5  6     char str[15] = "AASdf"; 7     cout<<strlen(str)<<endl; 8  9     system("pause");10     return 0;11 }
复制代码


0 0
原创粉丝点击