検索文字列、文字列の傍受、置換文字列でC ++

#include <iostream>
using namespace std;

/* 字符串查找 */ 
void findSubString(string str){
    // find()函数的使用,返回查找对象第一次出现的位置.  
    cout << str.find("fs") << endl;
    // rfind()函数的使用,返回查找对象最后出现的位置
    cout << str.rfind("s") << endl;
}

/* 字符串截取 */ 
void getSubString(string str){
    // substr(pos)函数的使用,返回从pos开始(包含pos位置的字符)所有的字符
    cout << str.substr(2) << endl;
    // substr(pos,n),返回从pos开始(包含pos位置的字符)n个字符
    cout << str.substr(2, 2) << endl;
}

/* 字符串替换 */ 
void replaceString(string str){
    // replace(pos,n,s1),用s1替换从pos开始的n个字符
    cout << str.replace(0,2,"xiaoming") << endl;
}

int main()
{
    string str = string("sdfsf");
    // findSubString(str);
    // getSubString(str);
    replaceString(str);
    return 0;
}

おすすめ

転載: www.cnblogs.com/komean/p/11109555.html
おすすめ