c ++ #include<string>


string基本概念

**本质**:
string 是c ++风格的字符串,是一个类对象
strings are objects that represent sequences of characters.
**特点**:
string管理char* 分配的内存空间,不用考虑越界.
封装了许多内部成员方法.

string 构造函数

/*
(1)default       string();
(2)copy 	     string (const string& str);
(3)substring     string (const string& str, size_t pos, size_t len = npos);
(4)from c-string string (const char* s);
(5)from buffer   string (const char* s, size_t n);
(6)fill 	     string (size_t n, char c);

size_t is an unsigned integral type.
*/

  string s0 ("Initial string");

  // constructors used in the same order as described above:
  string s1;
  string s2 (s0);
  string s3 (s0, 8, 3);
  string s4 ("A character sequence");
  string s5 ("Another character sequence", 12);
  string s6 (10, 'x');

s1:
s2: Initial string
s3: str
s4: A character sequence
s5: Another char
s6: xxxxxxxxxx

string 赋值操作

使用operator=()

/*
(1)string 	    string& operator= (const string& str);
(2)c-string 	string& operator= (const char* s);
(3)character 	string& operator= (char c);
*/
  
  str1 = "Test string: ";   // c-string
  str2 = 'x';               // single character
  str3 = str1 + str2;       // string

使用assign()

/*
(1)string 	 string& assign (const string& str);
(2)substring string& assign (const string& str, size_t subpos, size_t sublen);
(3)c-string  string& assign (const char* s);
(4)buffer 	 string& assign (const char* s, size_t n);
(5)fill 	 string& assign (size_t n, char c);
*/
  string str;
  string base="The quick brown fox jumps over a lazy dog.";

  // used in the same order as described above:
  str.assign(base);
  
  str.assign(base,10,9);            // "brown fox"
  
  str.assign("c-string");           // "c-string"
  
  str.assign("pangrams are cool",7);// "pangram"
 
  str.assign(10,'*');               // "**********"

string 字符串拼接(质变操作)

使用operator+=

/*
(1)string 	    string& operator+= (const string& str);
(2)c-string 	string& operator+= (const char* s);
(3)character 	string& operator+= (char c);
*/

  string name ("John");
  string family ("Smith");
  
  name += " K. ";         // c-string
  name += family;         // string
  name += '\n';           // character
 //name = John K. Smith

使用append

/*
(1)string 	   string& append (const string& str);
(2)substring   string& append (const string& str, size_t subpos, size_t sublen);
(3)buffer 	   string& append (const char* s, size_t n);
(4)c-string    string& append (const char* s);
(5)fill 	   string& append (size_t n, char c);
*/
  string str;
  string str2="Writing ";
  string str3="print 10 and then 5 more";

  // used in the same order as described above:
  str.append(str2);                       // "Writing "
  str.append(str3,6,3);                   // "10 "
  str.append("dots are cool",5);          // "dots "
  str.append("here: ");                   // "here: "
  str.append(10u,'.');                    // ".........."
//str = Writing 10 dots here: ..........

string 查找与替换

使用find()与rfind()

/*
(1)string 	  size_t find (const string& str, size_t pos = 0) const noexcept;

(2)c-string   size_t find (const char* s, size_t pos = 0) const;

(3)buffer 	  size_t find (const char* s, size_t pos, size_type n) const;

(4)character size_t find (char c, size_t pos = 0) const noexcept;
*/
	string  str="abcdefgde";
	int pos = str.find("de"); //3
	int pos=str.find("dg")    //-1

	int pos=str.rfind("de");  //7

	//在str中将str2替换成为"preposition"
	//str.replace(str.find(str2),str2.length(),"preposition");

使用replace()

/*
(1)string 	 string& replace (size_t pos,size_t len,const string& str);

(2)substring string& replace (size_t pos,size_t len,const string& str,size_t subpos, size_t sublen);

(3)fill      string& replace (size_t pos,size_t len,size_t n, char c);
*/

	string str="abcdefg";
	//从1号字符起,3个字符替换为1111
	str.replace(1,3,"1111");
	//str = a1111efg

总结:

  • find查找是从左往右,rfind是从右往左
  • find找到字符串后返回查找的第一个字符位置,找不到返回-1
  • replace替换,指定从哪个位置开始,多少个字符,替换成什么样的字符串

string 字符串比较

/*
(1)string 	    int compare (const string& str) const noexcept;

(2)substrings 	int compare (size_t pos, size_t len, const string& str) const;

(3)substrings   int compare (size_t pos, size_t len, const string& str,size_t subpos, size_t sublen) const;
*/
	string str1="hello";
	string str2="hello";

	if(str1.compare(str2)==0){}//等于
	if(str1.compare(str2) > 0){}//大于
	if(str1.compare(str2) < 0){}//小于

string 字符存取

使用operator与at()

/*
       char& operator[] (size_t pos);
 const char& operator[] (size_t pos) const;
*/
  string str ("Test string");
  for (int i=0; i<str.length(); ++i) //str.size()
  {
    cout << str[i];
    //cout << str.at(i); 取
    //str[0]="q";        存
  }

string 插入与删除

/*
(1)	string     string& insert (size_t pos, const string& str);

(2)substring   string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);

(3)c-string    string& insert (size_t pos, const char* s);

(4)buffer 	   string& insert (size_t pos, const char* s, size_t n);

(5)fill 	   string& insert (size_t pos, size_t n, char c);
*/

	string str = "hello";
	str.insert(1,"111");	//h111ello

/*
(1)sequence    string& erase (size_t pos = 0, size_t len = npos);
*/
	str.erase(1,3);			//hello

string 字串

/*
string substr (size_t pos = 0, size_t len = npos) const;
*/

	string str="abcdef";
	str.substr(1,3);	//bcd

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页