关于C++汉英混合的字符串寻找指定子字符串和截取

C++汉英混合的字符串截取

目前来看s.find(str)返回值都是-1,用s.substr(idx)从八位以后截取就会报错

#include
#include
#include
#include
#include< locale>
#include 
using namespace std;
void cut(wstring a) {
    wstring next;
    wstring h = L"荣耀";
    int head,end;
    wregex regexstr(L"^0.*");   //输入多行字符串处理,直到输入一个以0开头的字符串结束。
    wsmatch resault;
    if (regex_match(a, resault, regexstr)) cout<<"back"<else {
        getline(wcin, next);
        cut(next);        //用递归实现处理完一并输出
    }
    head = a.find(h);   //返回值恒为-1
    if(head>0)
        wcout<substr(head)<//只有把head改成具体值(<=8)可以截取
}
int main()
{
    wstring a;
    getline(wcin,a);
    cut(a);
}

img

刚学c++,一开始string处理不了汉字改为wstring,想先获取指定字符串位置再截取,但是s.find和s.substr现在都有问题,求拯救!!

img

#include <iostream>
#include <locale>
#include <regex>
#include <string>

using namespace std;

void cut(wstring a)
{
    wstring next;
    wstring const h = L"荣耀";
    size_t head;
    wregex const regexstr(
        L"^0.*"); // 输入多行字符串处理,直到输入一个以0开头的字符串结束。
    wsmatch resault;
    if (regex_match(a, resault, regexstr))
    {
        cout << "back" << endl;
    }
    else
    {
        getline(wcin, next);
        cut(next); // 用递归实现处理完一并输出
    }
    head = a.find(h); // 返回值恒为-1
    if (head != -1)
    {
        wcout << a.substr(head) << endl; // 只有把head改成具体值(<=8)可以截取
    }
}

auto main() -> int
{
    wstring a;
    wcin.imbue(locale(""));
    wcout.imbue(locale(""));
    getline(wcin, a);
    cut(a);
}


你输入的是什么? a