[英]How to split character using c++ win32 Api?
我正在使用c ++ win32 Api。
我想使用delimiter
分割字符。
像"CN=USERS,OU=Marketing,DC=RAM,DC=COM"
類的字符。
我想將字符分成第一個逗號(,)。這意味着我只需要
OU=Marketing,DC=RAM,DC=COM.
我已經嘗試過strtok
函數,但它僅拆分CN = USERS。
我怎樣才能做到這一點?
嘗試下面的代碼,您應該可以輕松獲取每個項目(用','分隔):strtok版本:
char domain[] = "CN=USERS,OU=Marketing,DC=RAM,DC=COM";
char *token = std::strtok(domain, ",");
while (token != NULL) {
std::cout << token << '\n';
token = std::strtok(NULL, ",");
}
std :: stringstream版本:
std::stringstream ss("CN=USERS,OU=Marketing,DC=RAM,DC=COM");
std::string item;
while(std::getline(ss, item, ','))
{
cout << item << endl;
}
看看std :: getline() http://en.cppreference.com/w/cpp/string/basic_string/getline
使用strchr
使其非常容易:
char domain[] = "CN=USERS,OU=Marketing,DC=RAM,DC=COM";
char *p = strchr(domain, ',');
if (p == NULL)
{
// error, no comma in the string
}
++p; // point to the character after the comma
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.