Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从C++;_C++ - Fatal编程技术网

C++ 如何从C++;

C++ 如何从C++;,c++,C++,我有这个字符串“67030416680001337D4912601000000000000” 我想取字母“D”之前的值。 还希望值仅在“D”后4位 我试过使用: string sPanNo = sTrkData.substr(0,17); string sExpDate = sTrkData.substr(18, 4); 但问题是字母“D”之前的字符串长度会有所不同。您可以使用string::find_first_of(),类似于: string::size_type dPos = sTrkD

我有这个字符串“67030416680001337D4912601000000000000”

我想取字母“D”之前的值。
还希望值仅在“D”后4位

我试过使用:

string sPanNo = sTrkData.substr(0,17);
string sExpDate = sTrkData.substr(18, 4);

但问题是字母“D”之前的字符串长度会有所不同。

您可以使用
string::find_first_of()
,类似于:

string::size_type dPos = sTrkData.find_first_of('D');
if (dPos != string::npos) {
    string sPanNo = sTrkData.substr(0, dPos);
    string sExpDate = sTrkData.substr(dPos + 1, 4);
}

您必须使用成员函数
find

比如说

std::string::size_type n = sTrkData.find( 'D' );

string sPanNo = sTrkData.substr( 0, n ); 

string sExpDate;

if ( n != std::string::npos )  sExpDate = sTrkData.substr( n + 1, 4 );

或使用<代码> <代码> C++ 11

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

using namespace std;

std::regex base_regex ( "(\\d+)D(\\d{4})\\d+" );
std::smatch base_match;

string numberstring = "67030416680001337D4912601000000000000";

if (std::regex_match(numberstring, base_match, base_regex))
{
    std::ssub_match base_sub_match = base_match[1];
    std::string number1 = base_sub_match.str();

    base_sub_match = base_match[2];
    std::string number2 = base_sub_match.str();

    cout << number1 << endl;
    cout << number2 << endl;
}
#包括
#包括
#包括
使用名称空间std;
std::regex base_regex(“(\\d+d(\\d{4})\\d+”);
std::smatch base_match;
字符串编号string=“67030416680001337D4912601000000000000”;
if(std::regex_匹配(numberstring,base_匹配,base_regex))
{
std::ssub_match base_sub_match=base_match[1];
std::string number1=base_sub_match.str();
基本匹配=基本匹配[2];
std::string number2=base_sub_match.str();

cout您可以使用正则表达式验证输入并在一个步骤中提取所需数据:

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

int main() {
    using namespace std;

    string input("67030416680001337D4912601000000000000");
    regex re("(\\d+)D(\\d{4})\\d+");

    match_results<string::const_iterator> m;
    if (regex_match(input, m, re)) {
        cout << m[1].str() << endl;
        cout << m[2].str() << endl;
    } else {
        cout << "invalid input\n";
    }
}
#包括
#包括
#包括
int main(){
使用名称空间std;
字符串输入(“67030416680001337D4912601000000000000”);
正则表达式re(“(\\d+d(\\d{4})\\d+”;
匹配结果m;
if(正则表达式匹配(输入、m、re)){

无法遍历字符串并按如下方式查找它:
std::find_if(sPanNo.begin(),sPanNo.end(),[](char c){return c='D';};
@Borgleader
std::find(sPanNo.begin(),sPanNo.end(),'D');
@Borgleader:Or
sPanNo.find')
,如果你不是一个过分迂回冗长的支持者。@NeilKirk肯定
find
也会起作用,就像
find\u first\u of
一样,因为这里不需要
find\u first\u of
的描述性更强。你可能不知道它的作用。