1. 程式人生 > >CPP 字串工具類

CPP 字串工具類

c++字串常用操作

  • 字串轉化為整形
int string_to_int(string s){
    return atoi(s.c_str());
}
  • 整形轉化為字串
string tostring(int a){
    stringstream ss;
    ss<<a;
    return ss.str();
}
  • 字元轉化為字串
string char2str(char a){
    string s(1, a);
}
  • 字串查詢函式
size_t find (const string& str, size_t pos = 0
) const; size_t find (const char* s, size_t pos = 0) const; size_t find (const char* s, size_t pos, size_t n) const; size_t find (char c, size_t pos = 0) const;
  • 字串erase函式
string&  erase (size_t pos = 0, size_t len = npos);
iterator erase (iterator p);
iterator erase (iterator first, iterator last);
  • 字串insert函式
 string& insert (size_t pos, const string& str);
 string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);
 string& insert (size_t pos, const char* s);
 string& insert (size_t pos, const char* s, size_t n);
 string& insert (size_t pos, size_t n, char
c); void insert (iterator p, size_t n, char c); iterator insert (iterator p, char c); template <class InputIterator> void insert (iterator p, InputIterator first, InputIterator last);
  • 插入一個字元到字串尾部
void push_back (char c);
  • 插入字串到尾部
string& append (const string& str);
string& append (const string& str, size_t subpos, size_t sublen);
string& append (const char* s);
string& append (const char* s, size_t n);
string& append (size_t n, char c);
template <class InputIterator>
   string& append (InputIterator first, InputIterator last);
  • 去查字串最後一個
void pop_back();