这篇博客内容包括:
- string基本操作
- 查找第n次出现字符“s_tofind”的位置;
- 按照某一pattern分割字符串;
- 将string数据转换成double数据.
代码如下:
1. string基本操作
//查找第n次出现字符“s_tofind”的位置
int findPos_n(string s, string s_tofind, int n);
//按照某一pattern分割字符串
void cutString(string str, vector<string> &split_str, string pattern);
//将string数据转换成double数据
double stringToDouble(string str);
int main(int argc, char **argv)
{
if(argc != 2)
{
cout << "Error! Syntax is : ./useString filename" << endl;
return 1;
}
// 读取文件特定行
string filename = string(argv[1]);
int line_n = 3;
string str_line = ReadLine(filename, line_n);//该函数在我的另一篇博客里面
cout << "the " << line_n << " line is : " << str_line << endl;
vector<string> split_str;
cutString("Lab205 is a big family !",split_str," ");
for (int i = 0; i < split_str.size(); ++i)
{
/* code */
cout << i << " : " << split_str[i] << endl;
}
//define
string str1 = "Lab205 is a big family ! " ;
//string str1 = "查找第n次出现字符“s_tofind”的位置";
string str2 ("I must study SLAM well !");
//copy string
string s_copy1 (str1); //deep copy
cout << "s_copy1 is : " << s_copy1 << endl ;//Lab205 is a big family !
// s_copy1.at(2) = 'c' ;
cout << "s_copy1.at(2) is : " << s_copy1.at(2) << endl;//b
string s_copy2 (str1,7);//from the 7th word to start copy
cout << "s_copy2 ( str1,7) is : " << s_copy2 << endl; //is a big family !
string s_copy3 (str1,3,6);//copy 6 words from the 3th word on
cout << "s_copy3 (str1,3,6) is : " << s_copy3 << endl << endl;
str1.~string();