如何查找某个字符串是否在本地C++中导入?代码示例将有很大帮助如何查找某个字符串是否在C++中导引
1
A
回答
7
3
2
尝试下面的代码 - 可以帮助。
_bstr_t sGuid(_T("guid to validate"));
GUID guid;
if(SUCCEEDED(::CLSIDFromString(sGuid, &guid))
{
// Guid string is valid
}
+0
当我只通过任何指导时,这似乎不起作用......它试图获得该分类显然不存在,因此失败。 – Sapna 2011-02-14 15:51:22
1
这里的情况下,你仍然需要一个代码示例:P
#include <ctype.h>
using namespace std;
bool isUUID(string uuid)
{
/*
* Check if the provided uuid is valid.
* 1. The length of uuids should always be 36.
* 2. Hyphens are expected at positions {9, 14, 19, 24}.
* 3. The rest characters should be simple xdigits.
*/
int hyphens[4] = {9, 14, 19, 24};
if (uuid.length() != 36)
{
return false;//Oops. The lenth doesn't match.
}
for (int i = 0, counter = 0; i < 36; i ++)
{
char var = uuid[i];
if (i == hyphens[counter] - 1)// Check if a hyphen is expected here.
{
// Yep. We need a hyphen here.
if (var != '-')
{
return false;// Oops. The character is not a hyphen.
}
else
{
counter++;// Move on to the next expected hyphen position.
}
}
else
{
// Nope. The character here should be a simple xdigit
if (isxdigit(var) == false)
{
return false;// Oops. The current character is not a hyphen.
}
}
}
return true;// Seen'em all!
}
0
这种方法利用了scanf
解析器和作品也以纯C:
#include <stdio.h>
#include <string.h>
int validateUUID(const char *candidate)
{
int tmp;
const char *s = candidate;
while (*s)
if (isspace(*s++))
return 0;
return s - candidate == 36
&& sscanf(candidate, "%4x%4x-%4x-%4x-%4x-%4x%4x%4x%c",
&tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp) == 8;
}
测试与如:
int main(int argc, char *argv[])
{
if (argc > 1)
puts(validateUUID(argv[1]) ? "OK" : "Invalid");
}
相关问题
- 1. VB,如何检查某个字符是否在字符串中
- 2. 如何检查字符串是否以C中的某个字符串开头?
- 3. 在字符串中查找某个字符的索引
- 4. 如何检查某些字符是否在字符串中?
- 5. 查找两个字符串是否字谜或者在C++中
- 6. 如何检查字符串是否包含某个字符?
- 7. 在字符串C++中查找索引
- 8. c#检查一个字符串是否有某个字
- 9. vb.net如何检查一个字符串是否有某个字
- 10. 如何在多个字符串中查找字符串中的某些字母?
- 11. 如何查找字符串中是否存在大写字符?
- 12. 在字符串c中查找单个字符的索引#
- 13. 如何检查某个字符串是否在字符串的开头?
- 14. 如何检查一个字符串中的某个位置是否为空c#
- 15. 如何检查一个字符串是否在C中的字符串数组?
- 16. 如何检查一个TextView是否包含某个字符串
- 17. 如何判断某个字母是否在字符串中 - javascript
- 18. 检查字符串变量是否为某个字符串值
- 19. 检查字符串是否是由某个字符
- 20. 如何查找某个字符串是否具有unicode字符(特别是双字节字符)
- 21. R:想要检查某个字符串中是否有任何元素出现在某个字符串中
- 22. 查找某个字符串在另一字符串在Matlab
- 23. 一个字符串查找是否有另一个字符串
- 24. 如何检查是否某个字符串或日期字符串被返回?
- 25. 如何查找变量是否包含某个字符? PHP
- 26. 如何找出部分字符串是否在引号中?
- 27. 如何检查某个字段是否在mongodb中索引?
- 28. 我如何知道某些字符是否在字符串中?
- 29. 查找字符串是否符合某种特定模式
- 30. 如何检查字符串是否包含C#中的字符?
我不做c + +但你不能使用这个:http://msdn.microsoft.com/en-us/library/system.guid.tryparse.aspx – Luis 2011-02-14 13:56:42