View Full Version : [SOLVED] How to split Glib::ustring?

March 1st, 2010, 04:20 PM
Folks, I can't find a split function in Glib::ustring.
What's the right way to do this? Can anyone please post a snippet?

ps: in case it matters, I need to split the Glib::ustring by the new line character "\n".

March 1st, 2010, 06:19 PM
I haven't testing this code, but I think you should be able to do something like:

// String to split
ustring aString("Some text \n the rest");

// Position of the new line in the string
int newLine = aString.find_first_of('\n');

// The first part up to the new line
ustring start = aString.substr(0, newLine);

// The part after the new line
ustring end = aString.substr(newLine, aString.length() - newLine);

If you have multiple lines, repeat the process on the created "end" variable until find_first_of() can't find a new line.

Like I said, I haven't tested it but this should be a decent starter.


ustring docs: http://library.gnome.org/devel/glibmm/unstable/classGlib_1_1ustring.html

March 1st, 2010, 06:23 PM
Thanks, that's what I was about to do anyway, I just hoped there's a simple way to do this since it's such a common task, in Java it's as simple as:
String[] myLines = allLines.split("\n");
And I'm done.
I hope there's some utility in glib that works with Glib::ustring.. otherwise it's a bit disappointing..

March 1st, 2010, 06:31 PM
Oh there we go, I just found a short working solution..

void test() {
Glib::ustring str = "Line 1\nLine 2\nLine 3\nLine 4";
std::vector<Glib::ustring> vLines = Glib::Regex::split_simple("\n", str); //THIS IS IT!!
std::cout << "size: " << vLines.size() << std::endl;
for(int i=0; i<vLines.size(); i++) {
std::cout << vLines.at(i) << std::endl;

March 1st, 2010, 06:33 PM
After looking at the docs it doesn't seem like there is one. If this is something that you want to do alot throughout your program it may be worth making your own string class which extends ustring with the added functionality. (Maybe returning a std::vector<String>).


You beat me. No need to create your own class then.

March 1st, 2010, 06:53 PM