簡體   English   中英

如何使用C ++ Win32 Api分割字符?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM