Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

c++ - 使用字符串定界符(标准C ++)在C ++中解析(拆分)字符串(Parse (split) a string in C++ using string delimiter (standard C++))

I am parsing a string in C++ using the following:

(我正在使用以下方法在C ++中解析字符串:)

string parsed,input="text to be parsed";
stringstream input_stringstream(input);

if(getline(input_stringstream,parsed,' '))
{
     // do some processing.
}

Parsing with a single char delimiter is fine.

(使用单个字符定界符进行解析就可以了。)

But what if I want to use a string as delimiter.

(但是,如果我想使用字符串作为分隔符怎么办?)

Example: I want to split:

(示例:我想拆分:)

scott>=tiger

with >= as delimiter so that I can get scott and tiger.

(用> =作为分隔符,这样我就可以得到Scott和Tiger。)

  ask by TheCrazyProgrammer translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the std::string::find() function to find the position of your string delimiter, then use std::string::substr() to get a token.

(您可以使用std::string::find()函数查找字符串定界符的位置,然后使用std::string::substr()获得令牌。)

Example:

(例:)

std::string s = "scott>=tiger";
std::string delimiter = ">=";
std::string token = s.substr(0, s.find(delimiter)); // token is "scott"
  • The find(const string& str, size_t pos = 0) function returns the position of the first occurrence of str in the string, or npos if the string is not found.

    (find(const string& str, size_t pos = 0)函数返回str中第一次出现str的位置,如果找不到该字符串,则npos 。)

  • The substr(size_t pos = 0, size_t n = npos) function returns a substring of the object, starting at position pos and of length npos .

    (substr(size_t pos = 0, size_t n = npos)函数返回对象的子字符串,从位置pos开始,长度为npos 。)


If you have multiple delimiters, after you have extracted one token, you can remove it (delimiter included) to proceed with subsequent extractions (if you want to preserve the original string, just use s = s.substr(pos + delimiter.length()); ):

(如果有多个定界符,则在提取了一个标记后,可以将其删除(包括定界符)以进行后续提取(如果要保留原始字符串,只需使用s = s.substr(pos + delimiter.length()); ):)

s.erase(0, s.find(delimiter) + delimiter.length());

This way you can easily loop to get each token.

(这样,您可以轻松地循环获取每个令牌。)

Complete Example (完整的例子)

std::string s = "scott>=tiger>=mushroom";
std::string delimiter = ">=";

size_t pos = 0;
std::string token;
while ((pos = s.find(delimiter)) != std::string::npos) {
    token = s.substr(0, pos);
    std::cout << token << std::endl;
    s.erase(0, pos + delimiter.length());
}
std::cout << s << std::endl;

Output:

(输出:)

scott
tiger
mushroom

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...