如何查找某个字符串是否在本地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. 如何检查字符串是否以C中的某个字符串开头?
- 2. 如何检查某些字符是否在字符串中?
- 3. 如何检查字符串是否包含某个字符?
- 4. vb.net如何检查一个字符串是否有某个字
- 5. 如何在多个字符串中查找字符串中的某些字母?
- 6. 在字符串c中查找单个字符的索引#
- 7. 如何检查某个字符串是否在字符串的开头?
- 8. 如何检查一个TextView是否包含某个字符串
- 9. 如何判断某个字母是否在字符串中 - javascript
- 10. 检查字符串变量是否为某个字符串值
- 11. 如何查找某个字符串是否具有unicode字符(特别是双字节字符)
- 12. 如何检查某个字段是否在mongodb中索引?
- 13. 我如何知道某些字符是否在字符串中?
- 14. 如何检查字符串是否包含C#中的字符?
- 15. 查找字符串是否在变量
- 16. 如何在PHP字符串中使用索引查找字符?
- 17. 计划 - 如何在字符串中查找字符索引
- 18. 如何检查字符串是否在字符串列表中
- 19. 查找字符串中是否有两个特定字符
- 20. 如何检查C中的字符串中的某些字符?
- 21. 如何查找子字符串是否在awk中的变量
- 22. 检查一个字符串中是否有某些字符在PHP中
- 23. C - 检查字符串是否是另一个字符串的子字符串
- 24. iOS/Objective-C:查找字符串索引
- 25. 如何检查一个字符串中的每个单词是否在另一个字符串中找到?
- 26. 查找字符串是否包含字
- 27. 如何找到C中字符串中的字符索引?
- 28. 如何查找c中另一个字符串中字符串给定字符的索引?
- 29. Oracle SQL Query查找字符串中是否存在字符
- 30. 如何找到一个字符串是否包含在另一个字符串
我不做c + +但你不能使用这个:http://msdn.microsoft.com/en-us/library/system.guid.tryparse.aspx – Luis 2011-02-14 13:56:42