字符串操作




Modifiers :
operator+=
Append to string  (public member function )
append
Append to string  (public member function )
push_back
Append character to string  (public member function )
assign
Assign content to string  (public member function )
insert
Insert into string  (public member function )
erase
Erase characters from string  (public member function )
replace
Replace portion of string  (public member function )
swap
Swap string values  (public member function )
pop_back 
Delete last character  (public member function )


Append to string 字符串的添加  str.append();  //
Extends the  string by appending additional characters at the end of its current value:


// appending to string
#include <iostream>
#include <string>

int main ()
{
  std::string str;
  std::string str2="Writing ";
  std::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.append(str3.begin()+8,str3.end());  // " and then 5 more"
  str.append<int>(5,0x2E);                // "....."

  std::cout << str << '\n';
  return 0;
}
Output:
Writing 10 dots here: .......... and then 5 more.....

Insert into string   插入字符。 str.insert();

Inserts additional characters into the  string right before the character indicated by  pos (or  p):
string (1)
 string& insert (size_t pos, const string& str);
substring (2)
 string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);
c-string (3)
 string& insert (size_t pos, const char* s);
buffer (4)
 string& insert (size_t pos, const char* s, size_t n);
fill (5)
 string& insert (size_t pos,   size_t n, char c);

std::string ::substr()   提取子字符串
Returns a newly constructed  string  object with its value initialized to a copy of a substring of this object.

The substring is the portion of the object that starts at character position  pos  and spans  len  characters (or until the end of the string, whichever comes first).

// string::substr
#include <iostream>
#include <string>

int main ()
{
  std::string str="We think in generalities, but we live in details.";
                                           // (quoting Alfred N. Whitehead)

  std::string str2 = str.substr (12,12);   // "generalities"

  unsigned pos = str.find("live");         // position of "live" in str

  std::string str3 = str.substr (pos);     // get from "live" to the end

  std::cout << str2 << ' ' << str3 << '\n';

  return 0;
}


Output:
generalities live in details.

字符串的搜索。std::string::find

Find content in string
Searches the  string for the first occurrence of the sequence specified by its arguments.
找到字符串在string中第一次出现的位置

When  pos is specified, the search only includes characters at or after position  pos, ignoring any possible occurrences that include characters before  pos.

Notice that unlike member  find_first_of, whenever more than one character is being searched for, it is not enough that just one of these characters match, but the entire sequence must match.

返回值return
The position of the first character of the first match.
If no matches were found, the function returns  string::npos.  没有找到 则返回  sting ::npos

size_t is an unsigned integral type.  无符号型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值