使用的strtok分割字符串 [英] Using strtok to split the string

查看:105
本文介绍了使用的strtok分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的char *字符串[30];
 焦炭Policystr [4096] =该|字符串|至|分裂;
 CHAR delims [] =|;
 INT I = 0; 字符串[我] = strtok的(Policystr,delims)
 而(字符串[I]!= NULL)
{
        MessageBoxA(NULL,串[I]中,stringsComparison,MB_OK);
        字符串[++ i] = strtok的(NULL,delims);
}
对于(INT J = 0; J< I; J ++)
 {
     MessageBoxA(NULL,字符串[J],弦,MB_OK);
 }

我是新来的C ++我得到的第一个循环中的所有字符串,如果我能够打印相同的第二循环,我不知道Ÿ我没有得到

在此先感谢


解决方案

您的问题被标记C ++,但你操纵像1985年(没有犯罪C程序员字符串数据,它只是使用的strtok的一般时下气馁)。由于您使用C ++,我会建议避免C库函数,而是使用C ++标准库中提供的功能。下面是拆分在C 的std ::字符串 ++的一种方式的完全独立的工作示例。我中C相当新手++所以这可能不是最有效的方法。这种方法的好处是:


  1. 内存管理是自动处理的,与您的输入或输出的大小程序规定没有任何限制。


  2. 没有使用旧的(和灰心)C库函数。


  3. 您不必修改原始输入数据分割字符串,这意味着你可以在const限定的输入操作。这是值得 strtok的是无法做到的,因为 strtok的修改原始输入。


  4. 可以,如果你有一个的char * 的char [] 缓冲仍然使用这种方法,因为你可以把它变成一个的std ::字符串(如果它不是由一个空字符结尾,你还需要给构造函数的缓冲区的长度)。


  5. 如果您想从向量,你可以使用的std ::字符串对象之一创建一个消息框.c_str()成员函数,例如:

      MessageBoxA(NULL,结果[I] .c_str(),弦,MB_OK);



 的#include<串GT;
#包括LT&;&iostream的GT;
#包括LT&;矢量>INT主要(无效)
{
    标准::字符串delims =|;
    标准::字符串policyStr =的|字符串|至|分裂;
    的std ::矢量<标准::字符串>结果;
    为size_t lastOffset = 0;    而(真)
    {
        size_t型偏移= policyStr.find_first_of(delims,lastOffset);
        results.push_back(policyStr.substr(lastOffset,偏移 - lastOffset));
        如果(偏移==标准::字符串::非营利组织)
            打破;
        其他
            lastOffset =偏移+ 1; //添加一个跳过定界符
    }    用于(为size_t我= 0; I< results.size();我++)
        性病::法院LT&;<结果[1] - ;<的std :: ENDL;    返回0;
}

 char *strings[30];
 char Policystr[4096] = "the|string|to|split"; 
 char delims[] = "|";
 int i = 0;

 strings[i] = strtok( Policystr, delims )
 while( strings[i] != NULL  ) 
{
        MessageBoxA(NULL,strings[i],"stringsComparison",MB_OK);
        strings[++i] = strtok( NULL, delims );          
}
for ( int j = 0; j < i; j++ ) 
 {
     MessageBoxA(NULL,strings[j],"strings",MB_OK);
 }

i am new to C++ i get all strings in first loop if i am able to print the same in second loop i don't know y i am not getting

thanks in advance

解决方案

Your question is tagged C++ but you are manipulating string data like a C programmer from 1985 (no offence, it's just that the use of strtok is generally discouraged nowadays). Since you're using C++, I would recommend avoiding C library functions and instead using functionality available in the C++ Standard Library. Here is a fully self-contained working example of one way to split a std::string in C++. I'm quite a novice at C++ so this may not be the most efficient way. The benefits to this approach are:

  1. Memory management is handled automatically, with no arbitrary limits imposed by your program on the size of input or output.

  2. No use of old (and discouraged) C library function.

  3. You don't have to modify the original input data to split the string, meaning you can operate on const-qualified input. This is something that strtok is unable to do, because strtok modifies the original input.

  4. You can still use this method if you have a char * or char [] buffer, because you can turn it into an std::string (if it is not terminated by a null character you will also need to give the constructor the length of the buffer).

  5. If you want to create a message box from one of the std::string objects in the vector, you can use the .c_str() member function, e.g.

    MessageBoxA(NULL, results[i].c_str(), "strings", MB_OK);
    


#include <string>
#include <iostream>
#include <vector>

int main(void)
{
    std::string delims = "|";
    std::string policyStr = "the|string|to|split";
    std::vector<std::string> results;
    size_t lastOffset = 0;

    while(true)
    {
        size_t offset = policyStr.find_first_of(delims, lastOffset);
        results.push_back(policyStr.substr(lastOffset, offset - lastOffset));
        if (offset == std::string::npos)
            break;
        else
            lastOffset = offset + 1; // add one to skip the delimiter
    }

    for (size_t i = 0; i < results.size(); i++)
        std::cout << results[i] << std::endl;

    return 0;
}

这篇关于使用的strtok分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆