我有這個字串post "ola tudo bem como esta" alghero.jpg,我想把它分成 3 部分post,ola tudo bem como esta(我不想要“”),alghero.jpg我在 c 中嘗試過,因為我是新手,不太擅長在 c 中編程,但它不起作用。在 C 中是否有更有效的方法來做到這一點?
程式:
int main()
{
char* token1 = new char[128];
char* token2 = new char[128];
char* token3 = new char[128];
char str[] = "post \"ola tudo bem como esta\" alghero.jpg";
char *token;
/* get the first token */
token = strtok(str, " ");
//walk through other tokens
while( token != NULL ) {
printf( " %s\n", token );
token = strtok(NULL, " ");
}
return(0);
}
uj5u.com熱心網友回復:
在 C 14 及更高版本中,您可以使用std::quoted從 any 中讀取帶引號的字串std::istream,例如std::istringstream,例如:
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
int main()
{
std::string token1, token2, token3;
std::string str = "post \"ola tudo bem como esta\" alghero.jpg";
std::istringstream(str) >> token1 >> std::quoted(token2) >> token3;
std::cout << token1 << "\n";
std::cout << token2 << "\n";
std::cout << token3 << "\n";
return 0;
}
uj5u.com熱心網友回復:
使用find找到2個引號的位置。使用substr獲得從索引0的字串第一次報價,第一次報價,以第二次試舉,并第二次試舉結束了。
std::string s = "post \"ola tudo bem como esta\" alghero.jpg";
auto first = s.find('\"');
if (first != s.npos) {
auto second = s.find('\"', first 1);
if (second != s.npos) {
std::cout << s.substr(0, first-1) << '\n';
std::cout << s.substr(first 1, second-first-1) << '\n';
std::cout << s.substr(second 2) << '\n';
}
}
輸出:
post
ola tudo bem como esta
alghero.jpg
uj5u.com熱心網友回復:
決議字串的一種選擇是使用正則運算式,例如:
#include <iostream>
#include <regex>
#include <string>
// struct to hold return value of parse function
struct parse_result_t
{
bool parsed{ false };
std::string token1;
std::string token2;
std::string token3;
};
// the parse function
auto parse(const std::string& string)
{
// this is a regex
// ^ match start of line
// (.*)\\\" matches any character until a \" (escaped ") and then escaped again for C string
// \w match one or more whitepsaces
// (.*)$ match 0 or more characters until end of string
// see it live here : https://regex101.com/r/XnkAZV/1
static std::regex rx{ "^(.*?)\\s \\\"(.*?)\\\"\\s (.*)$" };
std::smatch match;
parse_result_t result;
if (std::regex_search(string, match, rx))
{
result.parsed = true;
result.token1 = match[1];
result.token2 = match[2];
result.token3 = match[3];
}
return result;
}
int main()
{
auto result = parse("post \"ola tudo bem como esta\" alghero.jpg");
std::cout << "parse result = " << (result.parsed ? "success" : "failed") << "\n";
std::cout << "token 1 = " << result.token1 << "\n";
std::cout << "token 2 = " << result.token2 << "\n";
std::cout << "token 3 = " << result.token3 << "\n";
return 0;
}
uj5u.com熱心網友回復:
如果字串總是由一個空格分隔,您可以使用std::string::findstd::string::rfind`找到第一個空格和最后一個空格,在這些字符上拆分,并取消對中間字串的參考:
#include <iostream>
#include <tuple>
#include <string>
std::string unquote(const std::string& str) {
if (str.front() != '"' || str.back() != '"') {
return str;
}
return str.substr(1, str.size() - 2);
}
std::tuple < std::string, std::string, std::string> parse_triple_with_quoted_middle(const std::string& str) {
auto iter1 = str.begin() str.find(' ');
auto iter2 = str.begin() str.rfind(' ');
auto str1 = std::string(str.begin(),iter1);
auto str2 = std::string(iter1 1, iter2);
auto str3 = std::string(iter2 1, str.end() );
return { str1, unquote(str2), str3 };
}
int main()
{
std::string test = "post \"ola tudo bem como esta\" alghero.jpg";
auto [str1, str2, str3] = parse_triple_with_quoted_middle(test);
std::cout << str1 << "\n";
std::cout << str2 << "\n";
std::cout << str3 << "\n";
}
但是,您可能應該在上面添加更多的輸入驗證。
uj5u.com熱心網友回復:
您可以為此使用正則運算式:
- 重復搜索的模式是: 可選地以空格開頭
\s*;然后是([^\"]*)除引號之外的零個或多個字符(零個或多個,因為您可以一個接一個地使用多個引號);我們捕獲這個組(因此使用括號);最后,是參考\"還是|運算式的結尾$;我們不捕獲這個組(:?)。
我們使用std::regex來存盤模式,將其全部包裝在 中R"()",以便我們可以撰寫原始運算式。 - 該
while回圈做了幾件事情:它會搜索與下一場比賽regex_search,提取捕獲組,并更新輸入線,以便在下次搜索將開始在當前一個結束。matches是一個陣列,其第一個元素matches[0]是line匹配整個模式的部分,接下來的元素對應于模式的捕獲組。
[演示]
#include <iostream> // cout
#include <regex> // regex_search, smatch
int main() {
std::string line{"post \"ola tudo bem como esta\" alghero.jpg"};
std::regex pattern{R"(\s*([^\"]*)(:?\"|$))"};
std::smatch matches{};
while (std::regex_search(line, matches, pattern))
{
std::cout << matches[1] << "\n";
line = matches.suffix();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/396855.html
上一篇:當字符在字串中出現多次時進行標記
下一篇:從帶有“”的輸入行讀取
