Jump to content

Recommended Posts

Posted

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
Posted

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

 

  • 2 weeks later...
  • Solution
Posted

I am a little bit wary of modifying the way this works, but we can try it.

My job is to make tools you love, with the features you want, and performance you can't live without.

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.

×
×
  • Create New...