【问题标题】:How to split char pointer with multiple delimiters & return array of char pointers in c++?如何在 C++ 中使用多个分隔符拆分 char 指针并返回 char 指针数组?
【发布时间】:2015-08-07 17:18:43
【问题描述】:

在此问题Split char* to char * Array 的副本中,建议使用字符串而不是字符*。但我需要与 LPWSTR 合作。因为它是 char* 的 typedef,所以我更喜欢使用 char*。我尝试使用以下代码,但输出错误:

char**splitByMultipleDelimiters(char*ori,char deli[],int lengthOfDelimiterArray)
{
    char*copy = ori;
    char** strArray = new char*[10];
    int j = 0;
    int offset = 0;
    char*word = (char*)malloc(50);
    int length;
    int split = 0;
    for(int i = 0; i < (int)strlen(ori); i++)
    {
        for(int k = 0; (k < lengthOfDelimiterArray) && (split == 0);k++)
        {
            if(ori[i] == deli[k])
            {
                split = 1;
            }
        }
        if(split == 1)//ori[i] == deli[0]
        {
            length = i - offset;
            strncpy(word,copy,length);
            word[length] = '\0';
            strArray[j] = word;
            copy = ori + i + 1;
            //cout << "copy: " << copy << endl;
            //cout << strArray[j] << endl;
            j++;
            offset = i + 1;
            split = 0;
        }
    }
    strArray[j] = copy;
   // string strArrayToReturn[j+1];
    for(int i = 0; i < j+1; i++)
    {
        //strArrayToReturn[i] = strArray[i];
        cout << strArray[i] << endl;
    }
    return strArray;
}

void main()
{
        char*ori = "This:is\nmy:tst?why I hate";
        char deli[] = {':','?',' ','\n'};

        int lengthOfDelimiterArray = (sizeof(deli)/sizeof(*deli));
        splitByMultipleDelimiters(ori,deli,lengthOfDelimiterArray);
}

还有其他方法可以拆分LPWSTR吗?

【问题讨论】:

  • 只在需要的地方使用LPWSTR。为什么仅仅因为您稍后需要转换为 C 字符串(通常可以使用 c_str 完成)而使处理字符串变得更加困难?
  • LPWSTR 不是指向字符数组的指针。它是一个指向wide char 数组的指针。我的朋友,你将不得不将你的想法转移到 unicode 上。
  • “错误输出” 不是错误描述。由于没有人会读懂您的想法,因此要了解您期望的正确输出,这不是很有帮助。描述错误时,始终包括预期行为和观察到行为。

标签: c++ arrays pointers winapi


【解决方案1】:

等等,你在说什么?我在您的代码中的任何地方都没有看到 LPWSTR。您是否要转换 为 LPWSTR?如果是这样,则有一个 standard library 函数。还有一个standard library-based 用于拆分多个字符的解决方案。综上所述,您的代码可能如下所示:

#include <codecvt>
#include <cstdio>
#include <locale>
#include <sstream>
#include <string>

using std::string;
using std::wstring;

wstring toWide(const string &original)
{
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
    return converter.from_bytes(narrow_utf8_source_string);
}

std::vector<wstring> splitMany(const string &original, const string &delimiters)
{
    std::stringstream stream(original);
    std::string line;

    while (std::getline(original, line)) 
    {
        std::size_t prev = 0, pos;
        while ((pos = line.find_first_of(delimeters, prev)) != std::string::npos)
        {
            if (pos > prev)
                wordVector.push_back(line.substr(prev, pos-prev));
            prev = pos + 1;
        }
        if (prev < line.length())
            wordVector.push_back(line.substr(prev, std::string::npos));
    }
}

int main()
{
    string original = "This:is\nmy:tst?why I hate";
    string separators = ":? \n"

    std::vector<wstring> results = splitMany(original, separators);
}

此代码为这些函数使用标准库,并且比手动编写代码更不容易出错。

祝你好运!


编辑:要清楚,wstring == LPWSTR == wchar_t*

编辑 2:string 转换为 wstring

#include <codecvt>
#include <locale>
#include <string>

using std::string;
using std::wstring;

string toMultiByte(const wstring &original)
{
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
    return converter.to_bytes(original);
}

【讨论】:

  • 感谢您的回答。除了转换和拆分,还有什么标准函数可以拆分或解析LPWSTR?
  • @Janu 我不明白;您在问如何在不转换或拆分 wstring 的情况下转换或拆分它们?如果您要问如何从wstring 解析数字,那么是的,有一个standard library utility 可以从wstring 解析数字。基本上你可以用string 做的所有事情都可以用于wstring,请查看the C++ reference for wstring
  • 我需要解析 LPWSTR(例如:计算机名:Janu\n用户名:Janaki)并检索 Janu 作为计算机名和 Janaki 作为用户名。要像这样检索,我需要拆分 LPWSTR。
  • @Janu 然后你必须从std::wstring 转换为string。有关如何执行此操作的说明,请参阅我的更新答案。
  • @JamesKo 这是 Windows,源字符串不太可能是 UTF8。
猜你喜欢
  • 1970-01-01
  • 2021-01-26
  • 1970-01-01
  • 2016-06-10
  • 2018-06-27
  • 1970-01-01
  • 2015-10-26
  • 2011-02-07
  • 2011-03-21
相关资源
最近更新 更多