stringdemo.cpp

来自「C++&datastructure书籍源码,以前外教提供现在与大家共享」· C++ 代码 · 共 24 行

CPP
24
字号
#include <iostream>
#include <string>
using namespace std;

int main()
{ 
    string s = "I sing the body electric";
    
    cout << s << endl;
    cout << s.substr(2,4) << endl;
    cout << s.substr(s.find("electric")-5) << endl << endl;
 
    string copy(s);
    int bodyPos = copy.find("body");
    copy.replace(bodyPos,copy.length(),"blues");
    cout << copy << endl << endl;    
    
    cout << "search for chars/strings" << endl;
    cout << "first e at " << s.find('e')  << endl;
    cout << "last e at "  << s.rfind('e') << endl;
    cout << "first z at " << s.find('z')  << endl;
    cout << "space after body "   << s.find(" ", bodyPos) << endl; 
    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?