1

我如何定义一个接受字符串的方法,比如说

asdfgsdfg:ijtij ijdfgh ija::saf 1999 bp

并返回子字符串

ijtij ijdfgh ija::saf 1999

也就是说,我想要在第一个冒号实例之间的所有数据,直到在字符串末尾找到 bp 字符组合。

4

3 回答 3

7
string original = "asdfgsdfg:ijtij ijdfgh ija::saf 1999 bp";

size_t s = original.find(":");
size_t e = original.find("bp", s);
string sub = original.substr(s + 1, e - s -1);
cout << sub ;
于 2013-04-11T20:47:10.253 回答
5

用于std::string::find定位第一个冒号和冒号后第一个出现的“bp”。然后用于std::string::substr获取这些位置之间的子字符串。

#include <iostream>
#include <string>

int main()

{
    std::string str = "asdfgsdfg:ijtij ijdfgh ija::saf 1999 bp";
    int start = str.find(":");
    int end = str.find("bp", start);
    std::string substring;
    if (start != std::string::npos && end != std::string::npos)
    {
       substring = str.substr(start + 1, end - start - 1);
    }
    else
    {
       // Whatever you want to do if the markers are missing.
    }
    std::cout << substring;
}

在这里输出。

编辑:根据 MooingDuck 对 stardust_ 答案的评论,目前尚不清楚您对“bp”标记的意图是什么。第一个找到的“bp”实例是否结束子字符串,或者子字符串总是在原始字符串的最后两个字符之前结束?(在这种情况下,它们是否是“bp”无关紧要)。如果是后者,那么修改如下:

#include <iostream>
#include <string>

int main()

{
    std::string str = "asdfgsdfg:ijtij ijdfgh ija::saf 1999 bp";
    int start = str.find(":");
    int end = str.length() - 2; // Changed here
    std::string substring;
    if (start != std::string::npos && end != std::string::npos)
    {
       substring = str.substr(start + 1, end - start - 1);
    }
    else
    {
       // Whatever you want to do if the markers are missing.
    }
    std::cout << substring;
}

在这里输出。

于 2013-04-11T20:45:50.610 回答
1

给你一个完整的函数,可以从 std::string 中检索子字符串。还包括有关此功能的良好文档。

#pragma region INFO
/*
 * @ FUNCTION: GetSubStrBetween
 *
 * @ PARAMETER(s):
 *      [1st] std::string &in_Str = This paramter takes in a std::string, which
 *            is the string that contains the unknown sub-string.
 *
 *      [2nd] std::string in_A = This parameter takes in a std::string, which
 *            will be the beginning point of the unknown sub-string.
 *
 *      [3rd] std::string in_B = This parameter takes in a std::string, which
 *            happens to be the ending point of the unknown sub-string.
 *
 *      [4th] std::string in_out_SubStr = This parameter takes in a std::string,
 *            which will contain the unknown sub-string.
 *
 *      [5th] bool in_opt_Append = This optional* parameter takes in either a true
 *            or false value. If in_opt_Append = true, in_out_SubStr (see 4th
 *            param.) will append the unknown sub-str. Else, in_out_SubStr will be
 *            equal to the unknown sub-str. Note: The default value is false.
 *
 * @ RETURN VALUE(s):
 *      false = This function returns a false value because...
 *              - The length of in_Str/in_A/in_B is equal to 0.
 *              - The length of in_A/in_B is greater than or equal to the length
 *                of in_Str.
 *              - The length of in_Str is smaller than length of in_A + the length
 *                of in_B.
 *              - Unable to find in_A/in_B.
 *      true = Successfully found and return the unknown sub-str to in_out_SubStr.
 */
#pragma endregion INFO

bool GetSubStrBetween( std::string &in_Str,
                       std::string in_A,
                       std::string in_B,
                       std::string &in_out_SubStr,
                       bool in_opt_Append = false )
{
    //# Check for possible errors.
    if( in_A.length() == 0 ||
        in_B.length() == 0 ||
        in_Str.length() == 0 ||
        in_Str.length() <= in_A.length() ||
        in_Str.length() <= in_B.length() ||
        in_Str.length() < in_A.length() + in_B.length() )
    {   return false; }

    //# Try to find the positions of in_A and in_B within in_Str.
    const int A_Pos = in_Str.find( in_A );
    const int B_Pos = in_Str.find( in_B );

    //# Check if in_A and in_B does exist within in_Str.
    if( A_Pos < 0 || B_Pos < 0 )
    {   return false; }

    //# Retrieve the unknown-substr and either append it to in_out_SubStr
    //  or make in_out_SubStr equal to the unknown sub-string.
    ( in_opt_Append == true ) ?
        ( in_out_SubStr = in_Str.substr( A_Pos + 1, B_Pos - 1 - A_Pos ) ) :
        ( in_out_SubStr += in_Str.substr( A_Pos + 1, B_Pos - 1 - A_Pos ) );

    //# Success!
    return true;
};

不要忘记包含字符串。

这是基于上述问题的使用示例:

std::string Str = "asdfgsdfg:ijtij ijdfgh ija::saf 1999 bp";
std::string SubStr = "";

if( GetSubStrBetween( Str, ":", "bp", SubStr ) ) {
    std::cout << std::endl << GetSubStrBetween << std::endl;
} else { std::cout << "\nGetSubStrBetween(...) has failed!\n"; }
于 2013-04-11T22:37:46.913 回答