如何查找某個字符串是否在本地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