如何使用 c++ win32 API 拆分字符

How to split character using c++ win32 Api?

本文关键字:拆分 字符 API win32 何使用 c++      更新时间:2023-10-16

我正在使用c ++ win32 api。

我想使用 delimiter 拆分角色。

那个角色像"CN=USERS,OU=Marketing,DC=RAM,DC=COM".

我想将字符拆分为第一个逗号(,)之后,这意味着我只需要

OU=Marketing,DC=RAM,DC=COM.

我已经尝试了strtok功能,但它只拆分了CN = USERS。

我怎样才能做到这一点?

尝试下面的代码,您应该能够轻松获取每个项目(用","分隔):斯特托克版本:

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 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