用冒号和空格拆分字符串?
Splitting string with colons and spaces?
所以我已经使我的代码可以用于分隔字符串:
String c;
for (int j = 0 ; j < count; j++) {
c += ip(ex[j]);
}
return c;
}
void setup() {
Serial.begin(9600);
}
我在这方面运气不佳,如有任何帮助,我们将不胜感激!
如果您的编译器支持 C++11,我建议使用 <regex>
库。
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <regex>
const std::regex ws_re(":| +");
void printTokens(const std::string& input)
{
std::copy( std::sregex_token_iterator(input.begin(), input.end(), ws_re, -1),
std::sregex_token_iterator(),
std::ostream_iterator<std::string>(std::cout, "\n"));
}
int main()
{
const std::string text1 = "...:---:...";
std::cout<<"no whitespace:\n";
printTokens(text1);
std::cout<<"single whitespace:\n";
const std::string text2 = "..:---:... ..:---:...";
printTokens(text2);
std::cout<<"multiple whitespaces:\n";
const std::string text3 = "..:---:... ..:---:...";
printTokens(text3);
}
库的描述在 cppreference. If you are not familiar with regular expressions, the part in the code above const std::regex ws_re(":| +");
means that there should be either ':' symbol or (or
in regular expressions denoted by pipe symbol '|') any amount of whitespaces ('+' stands for 'one or more symbol that stands before the plus sign'). Then one is able to use this regular expression to tokenize any input with std::sregex_token_iterator
. For more complex cases than whitespaces, there is wonderful regex101.com。
我能想到的唯一缺点是正则表达式引擎可能比简单的手写分词器慢。
我只想在您的分词器中添加一个分隔符。从 strtok() description 第二个参数“是包含定界符的 C 字符串。这些可能因调用而异”。
因此,在您的标记化中添加一个 'space' 定界符:来自您的标记的 ex[i] = strtok(NULL, ": ");
trim any whitespace,并丢弃所有空标记。最后两个不是必需的,因为分隔符不会成为您收集的标记的一部分。
所以我已经使我的代码可以用于分隔字符串:
String c;
for (int j = 0 ; j < count; j++) {
c += ip(ex[j]);
}
return c;
}
void setup() {
Serial.begin(9600);
}
我在这方面运气不佳,如有任何帮助,我们将不胜感激!
如果您的编译器支持 C++11,我建议使用 <regex>
库。
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <regex>
const std::regex ws_re(":| +");
void printTokens(const std::string& input)
{
std::copy( std::sregex_token_iterator(input.begin(), input.end(), ws_re, -1),
std::sregex_token_iterator(),
std::ostream_iterator<std::string>(std::cout, "\n"));
}
int main()
{
const std::string text1 = "...:---:...";
std::cout<<"no whitespace:\n";
printTokens(text1);
std::cout<<"single whitespace:\n";
const std::string text2 = "..:---:... ..:---:...";
printTokens(text2);
std::cout<<"multiple whitespaces:\n";
const std::string text3 = "..:---:... ..:---:...";
printTokens(text3);
}
库的描述在 cppreference. If you are not familiar with regular expressions, the part in the code above const std::regex ws_re(":| +");
means that there should be either ':' symbol or (or
in regular expressions denoted by pipe symbol '|') any amount of whitespaces ('+' stands for 'one or more symbol that stands before the plus sign'). Then one is able to use this regular expression to tokenize any input with std::sregex_token_iterator
. For more complex cases than whitespaces, there is wonderful regex101.com。
我能想到的唯一缺点是正则表达式引擎可能比简单的手写分词器慢。
我只想在您的分词器中添加一个分隔符。从 strtok() description 第二个参数“是包含定界符的 C 字符串。这些可能因调用而异”。
因此,在您的标记化中添加一个 'space' 定界符:来自您的标记的 ex[i] = strtok(NULL, ": ");
trim any whitespace,并丢弃所有空标记。最后两个不是必需的,因为分隔符不会成为您收集的标记的一部分。