我想测试std::string
为包含任何范围的数字,例如5 to 35
在std::string s = "XDGHYH20YFYFFY"
会有功能,或者我将不得不将数字转换为字符串,然后使用循环找到每一个?检查字符串是否包含一个数字范围
4
A
回答
9
我可能会使用该处理一切,除了数字为白色空间的区域,并从与该区域充满一个字符串流读取的数量和检查,如果他们在范围:
#include <iostream>
#include <algorithm>
#include <locale>
#include <vector>
#include <sstream>
struct digits_only: std::ctype<char>
{
digits_only(): std::ctype<char>(get_table()) {}
static std::ctype_base::mask const* get_table()
{
static std::vector<std::ctype_base::mask>
rc(std::ctype<char>::table_size,std::ctype_base::space);
std::fill(&rc['0'], &rc['9'], std::ctype_base::digit);
return &rc[0];
}
};
bool in_range(int lower, int upper, std::string const &input) {
std::istringstream buffer(input);
buffer.imbue(std::locale(std::locale(), new digits_only()));
int n;
while (buffer>>n)
if (n < lower || upper < n)
return false;
return true;
}
int main() {
std::cout << std::boolalpha << in_range(5, 35, "XDGHYH20YFYFFY");
return 0;
}
2
尽管有些人会立即转到正则表达式,但我认为您最好的选择是混合解决方案。使用REGEX查找数字,然后解析它们并查看它们是否在范围内。
在C#中是这样的。不确定在C++中可以使用哪些正则表达式库。
using System.Text.RegularExpressions.Regex;
using System.Text.RegularExpressions.Match;
using System.Text.RegularExpressions.MatchCollection;
private static const Regex NUMBER_REGEX = new Regex(@"\d+")
public static bool ContainsNumberInRange(string str, int min, int max)
{
MatchCollection matches = NUMBER_REGEX.Matches(str);
foreach(Match match in matches)
{
int value = Convert.ToInt32(match.Value);
if (value >= min && value <= max)
return true;
}
return false;
}
0
如果您正在搜索范围内的所有数字,请在循环中使用string :: find函数。下面是该函数的引用:
http://www.cplusplus.com/reference/string/string/find/
而且你想在字符串中使用的数字只有一次?例如SDFSD256fdsfs会给你数字2 5 6 25 56 256,如果你没有在每次比赛结束后将它们从字符串中删除。
0
您可以用stringstream
对象迭代执行此操作。
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
void check (std::string& s) {
std::stringstream ss(s);
std::cout << "Searching string: " << s << std::endl;
while (ss) {
while (ss && !isdigit(ss.peek()))
ss.get();
if (! ss)
break;
int i = 0;
ss >> i;
std::cout << " Extraced value: " << i << std::endl;
}
}
int main() {
std::string s1 = "XDGHYH20YFYFFY";
std::string s2 = "20YF35YFFY100";
check (s1);
check (s2);
return 0;
}
产量:
Searching string: XDGHYH20YFYFFY
Extraced value: 20
Searching string: 20YF35YFFY100
Extraced value: 20
Extraced value: 35
Extraced value: 100
添加参数到check
功能,限制接受的将是微不足道的结果。
相关问题
- 1. 检查一个字符串是否包含数字和字母
- 2. Applescript:检查一个字符串是否包含空字符串?
- 3. 检查一个字符串是否包含数字
- 4. F#检查一个字符串是否只包含数字
- 5. Java,如何检查一个字符串是否包含数字?
- 6. 检查包含数字的字符串值的范围
- 7. 检查一个字符串是否只包含特殊字符
- 8. PHP检查字符串是否包含一些字母/数字
- 9. 检查一个字符串是否不包含一些文字
- 10. 检查字符串是否包含字(不是子字符串!)
- 11. 检查是否字符串包含“HTTP://”
- 12. 检查行是否包含字符串
- 13. 如何检查一个字符串是否包含一个子字符串 - mysql
- 14. 有效检查一个字符串是否包含另一个字符串
- 15. 检查一个字符串是否包含另一个字符串
- 16. 检查一个字符串是否包含任何字符串数组元素
- 17. 检查一个字符串是否包含整数
- 18. 检查一个字符串是否包含一个int
- 19. 检查一个列表是否包含一个字符串
- 20. 检查一对字符串是否包含相同的字符?
- 21. 如何检查字符串是否包含某个字符?
- 22. 检查2个字符串是否包含相同的字符?
- 23. Java:检查字符串是否包含多个字符
- 24. 如何检查一个字符串是否包含任何一些字符串
- 25. Javascript - 如何检查一个字符串是否包含多个子字符串
- 26. 检查字符串是否包含数字和符号
- 27. 如何检查是否字符串包含数字符号
- 28. 检查字符串是否包含字母数字
- 29. 检查一条消息是否包含一个字符串
- 30. 如何检查一个TextView是否包含某个字符串
我想你在这里需要的是一个正则表达式。 – 2010-06-22 18:35:11
您是否试图在范围内找到至少一个数字或范围内的所有数字?如果这是第一个我认为使用正则表达式会更好,但如果你将检查范围内的所有数字比使用查找功能。 – 2010-06-22 18:44:33