Jump to content

WString Split bugs


Dreikblack
 Share

Go to solution Solved by Josh,

Recommended Posts

1. If after delimiter 1 symbol in result will be empty string

2. Empty string is not shown if delimiter was last symbol. Could be optionable with bool param if intended

 

#include "UltraEngine.h"

using namespace UltraEngine;

int main(int argc, const char* argv[]) {
    WString s = "\n1";
    vector<WString> sarr = s.Split("\n");

    for (auto s : sarr) {
        if (s.empty()) {
            Print("Empty");
        } else {
            Print(s);
        }
    }

    Print("2nd case:");
    s = "11\n";
    sarr = s.Split("\n");
    for (auto s : sarr) {
        if (s.empty()) {
            Print("Empty");
        } else {
            Print(s);
        }
    }
    return 0;
}

 

  • Thanks 1
Link to comment
Share on other sites

Made own function for now:

#include "UltraEngine.h"

using namespace UltraEngine;


static vector<WString> Split(WString const& text, WString const& delimiter) {
    WString tempText = text;
    vector<WString> result;
    while (!tempText.empty()) {
        int delimiterIndex = tempText.Find(delimiter);
        if (delimiterIndex == -1) {
            result.push_back(tempText);
            break;
        }
        result.push_back(tempText.Left(delimiterIndex));
        tempText = tempText.Right((int)tempText.length() - delimiterIndex - (int)delimiter.length());
        if (tempText.empty() && delimiterIndex != -1) {
            result.push_back("");
        }
    }
    return result;
}

static void PrintArr(vector<WString> sarr) {
    for (auto s : sarr) {
        if (s.empty()) {
            Print("Empty");
        } else {
            Print(s);
        }
    }
}


int main(int argc, const char* argv[]) {
    PrintArr(Split("\n1", "\n"));
    Print(" ");
    PrintArr(Split("1\n", "\n"));
    Print(" ");
    PrintArr(Split("\n1\n", "\n"));
    Print(" ");
    PrintArr(Split("1\n1\n", "\n"));
    return 0;
}

 

Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...