string

C++之STL -- string

1.string和char *的区别

  (1)string是一个类,char *是一个指向字符的指针。

  (2)string不用考虑内存释放和越界。

  (3)string提供了一系列字符串操作函数:find,copy,erase,replace,insert等。

 

2.string的构造函数及遍历

 

 
  1. #include <iostream>

  2. #include <string>

  3. using namespace std;

  4. int main()

  5. {

  6. /* 字符串的四种构造方法 */

  7. string s1 = "hello";

  8. string s2("haha");

  9. string s3 = s2;

  10. string s4(5,'a');

  11. cout << s4 << endl; //aaaaa

  12.  
  13. /* 字符串遍历的方法 */

  14. //1.数组方式

  15. for(int i=0;i<s1.length();i++)

  16. {

  17. cout << s1[i] << " ";

  18. }

  19. cout << endl;

  20. //2.迭代器

  21. for(string::iterator it = s1.begin();it!=s1.end();it++)

  22. {

  23. cout << *it << " ";

  24. }

  25. cout << endl;

  26.  
  27. return 0;

  28. }

 

 

3.string类存取字符的操作

 

 
  1. #include <iostream>

  2. #include <string>

  3. using namespace std;

  4. int main()

  5. {

  6. string s1 = "hello";

  7. for(int i=0;i<s1.length();i++)

  8. {

  9. cout << s1.at(i) << " "; //抛出异常

  10. }

  11. return 0;

  12. }

 


4.字符指针和string的对换

 

 

string

 

5.连接字符串

 
  1. #include <iostream>

  2. #include <string>

  3. using namespace std;

  4. int main()

  5. {

  6. string s1 = "hello";

  7. string s2 = "world";

  8. s1 = s1 + s2;

  9. cout << s1 << endl;

  10.  
  11. string s3 = "hello";

  12. string s4 = "c++";

  13. s3.append(s4);

  14. cout << s3 << endl;

  15. return 0;

  16. }



6.字符串查找和替换(重点)6.字符串查找和替换(重点)

 

API如下:

string

string

 
  1. #include <iostream>

  2. #include <string>

  3. using namespace std;

  4. int main()

  5. {

  6. string s1 = "hello Java,hello C#,hello C++,hello Python";

  7. //第一次出现hello的index

  8. int index = s1.find("hello",0);

  9. cout << index << endl;

  10.  
  11. //求hello出现的次数以及每一次出现的数组下标

  12. int offindex = s1.find("hello",0);

  13. while (offindex != string::npos)

  14. {

  15. cout << offindex << endl;

  16. offindex = offindex+1;

  17. offindex = s1.find("hello",offindex);

  18. }

  19.  
  20. return 0;

  21. }



7.string的典型操作:删除和插入

 

  (1)删除操作:

 

 
  1. #include <iostream>

  2. #include <algorithm>

  3. #include <string>

  4. using namespace std;

  5. int main()

  6. {

  7. string s1 = "hello1 hello2 hellol";

  8. string::iterator it = find(s1.begin(),s1.end(),'1');

  9. if (it != s1.end())

  10. {

  11. s1.erase(it);

  12. }

  13. cout << s1 << endl;

  14.  
  15. s1.erase(s1.begin(),s1.end()); //全部删除

  16. cout << s1.length() << endl;

  17. return 0;

  18. }

 

 

  (2)插入操作:

 

 
  1. #include <iostream>

  2. #include <algorithm>

  3. #include <string>

  4. using namespace std;

  5. int main()

  6. {

  7. string s1 = "aaa";

  8. s1.insert(0,"hahaha"); //头部插入

  9. s1.insert(s1.length(),"---"); //尾部插入

  10. cout << s1 << endl;

  11. return 0;

  12. }


8.大小写转换

 

 

 
  1. #include <stdio.h>

  2. #include <string.h> //strlen

  3. #include <ctype.h> //toupper

  4. using namespace std;

  5. int main()

  6. {

  7. int i;

  8. char s[] = "HELLO WORLD";

  9. for(i=0;i<strlen(s);i++)

  10. {

  11. s[i] = tolower(s[i]);

  12. }

  13. printf("%s\n",s);

  14. return 0;

  15. }