string 总结

来源:互联网 发布:sql 恢复数据库 编辑:程序博客网 时间:2024/05/29 05:04

void  string_replace(string & bigstr,const string &srcstr,const string& decstr)
{
 string::size_type pos  = 0;
 string::size_type strlen =srcstr.size();
 string::size_type declen =decstr.size();

 while( (pos= bigstr.find(srcstr,pos)) != string::npos)
 {
  bigstr.replace(pos,strlen +5,decstr);   // 这个是用decstr 代替多少个字符,无论多少个字符,换成的都是decstr。
  pos += declen;
 }
}


#ifdef _FINDFIRST_
const string str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
void main(void)
{
 string str2="/*------------jing   ----   peng   */";
 string::size_type pos  = str2.find_first_of(str);
 if ( pos == string::npos)
 {
  cout<<"not find the str"<<endl;
 }
 string::size_type endpos = str2.find_last_of(str);
 if ( endpos == string::npos)
 {
  cout<<"not find the str at end"<<endl;
 }
 string str3 = str2.substr(pos,endpos-pos+1 );
 cout<<str3<<endl;
 getch();
}

int string2int(const string & str)
{
 return atoi(str.c_str());
}

double string2double(const string & str)
{
 return atof(str.c_str());
}


#include <iostream>
#include <sstream>
#include <string>

template <class T>
void convertFromString(T &, const std::string &);

int main()
{
  std::string s("123");
 
  // Convert std::string to int
     int i = 0;
  convertFromString(i,s);
     std::cout << i << std::endl;
  // Convert std::string to double
  double d = 0;
  convertFromString(d,s);
     std::cout << d << std::endl;
  return 0;
}

template <class T>
void convertFromString(T &value, const std::string &s)

 std::stringstream ss(s);
    ss >> value;
}
#endif