标记一个字符串意味着根据某些分隔符拆分一个字符串。有很多方法可以标记字符串。本文解释了其中四个:
null
使用stringstream
A. stringstream 将字符串对象与流相关联,从而可以像读取流一样读取字符串。
下面是C++实现:
C++
// Tokenizing a string using stringstream #include <bits/stdc++.h> using namespace std; int main() { string line = "GeeksForGeeks is a must try" ; // Vector of string to save tokens vector <string> tokens; // stringstream class check1 stringstream check1(line); string intermediate; // Tokenizing w.r.t. space ' ' while (getline(check1, intermediate, ' ' )) { tokens.push_back(intermediate); } // Printing the token vector for ( int i = 0; i < tokens.size(); i++) cout << tokens[i] << '' ; } |
输出
GeeksForGeeksisamusttry
使用 斯特托克()
// Splits str[] according to given delimiters.// and returns next token. It needs to be called// in a loop to get all tokens. It returns NULL// when there are no more tokens.char * strtok(char str[], const char *delims);
下面是C++实现:
C++
// C/C++ program for splitting a string // using strtok() #include <stdio.h> #include <string.h> int main() { char str[] = "Geeks-for-Geeks" ; // Returns first token char *token = strtok (str, "-" ); // Keep printing tokens while one of the // delimiters present in str[]. while (token != NULL) { printf ( "%s" , token); token = strtok (NULL, "-" ); } return 0; } |
输出
GeeksforGeeks
strtok()的另一个示例:
C
// C code to demonstrate working of // strtok #include <string.h> #include <stdio.h> // Driver function int main() { // Declaration of string char gfg[100] = " Geeks - for - geeks - Contribute" ; // Declaration of delimiter const char s[4] = "-" ; char * tok; // Use of strtok // get first token tok = strtok (gfg, s); // Checks for delimiter while (tok != 0) { printf ( " %s" , tok); // Use of strtok // go through other tokens tok = strtok (0, s); } return (0); } |
输出
Geeks for geeks Contribute
使用 strtok_r()
就像C中的strtok()函数一样, strtok_r() 执行将字符串解析为标记序列的相同任务。strtok_r()是strtok()的可重入版本。
有两种方法可以称为strtok_r()
// The third argument saveptr is a pointer to a char * // variable that is used internally by strtok_r() in // order to maintain context between successive calls// that parse the same string.char *strtok_r(char *str, const char *delim, char **saveptr);
下面是一个简单的C++程序,展示了Strutokr()的用法:
C++
// C/C++ program to demonstrate working of strtok_r() // by splitting string based on space character. #include<stdio.h> #include<string.h> int main() { char str[] = "Geeks for Geeks" ; char *token; char *rest = str; while ((token = strtok_r(rest, " " , &rest))) printf ( "%s" , token); return (0); } |
输出
GeeksforGeeks
使用std::sregx_token_迭代器
在这种方法中,标记化是在正则表达式匹配的基础上完成的。更好地用于需要多个分隔符的用例。
下面是一个简单的C++程序来显示STD::SrEXExth-TokKyTyrter的用法:
C++
// CPP program for above approach #include <iostream> #include <regex> #include <string> #include <vector> /** * @brief Tokenize the given vector according to the regex * and remove the empty tokens. * * @param str * @param re * @return std::vector<std::string> */ std::vector<std::string> tokenize( const std::string str, const std::regex re) { std::sregex_token_iterator it{ str.begin(), str.end(), re, -1 }; std::vector<std::string> tokenized{ it, {} }; // Additional check to remove empty strings tokenized.erase( std::remove_if(tokenized.begin(), tokenized.end(), [](std::string const & s) { return s.size() == 0; }), tokenized.end()); return tokenized; } // Driver Code int main() { const std::string str = "Break string a,spaces,and,commas"; const std::regex re(R "([s|,]+)" ); // Function Call const std::vector<std::string> tokenized = tokenize(str, re); for (std::string token : tokenized) std::cout << token << std::endl; return 0; } |
输出
Breakstringaspacesandcommas
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END