Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Looking for examples to split string into container
Looking for examples to split string into container [message #55309] Wed, 28 October 2020 17:15 Go to next message
sinpeople is currently offline  sinpeople
Messages: 29
Registered: October 2020
Location: Singapore
Promising Member

Hi Folks,

I am looking for a sample code to split a string according to a set of predefined deliminator, which is very similar to the following C# code.
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };

string text = "one\ttwo three:four,five six seven";
System.Console.WriteLine($"Original text: '{text}'");

string[] words = text.Split(delimiterChars);
System.Console.WriteLine($"{words.Length} words in text:");

foreach (var word in words)
{
    System.Console.WriteLine($"<{word}>");
}


Thank you so much!

Best Regards
David WANG
Re: Looking for examples to split string into container [message #55310 is a reply to message #55309] Wed, 28 October 2020 18:41 Go to previous messageGo to next message
Klugier is currently offline  Klugier
Messages: 1075
Registered: September 2012
Location: Poland, Kraków
Senior Contributor
Hello David,

Use global Split function. For more info check following example and read this documentation site.

I think this should be straight forward:
auto numbers = Split("one,two,,three", ','); // <- Type is Vector<String>, Upp::Split if not in namespace
for (const auto& number : numbers) {
   // number is string...
}


Klugier


U++ - one framework to rule them all.

[Updated on: Wed, 28 October 2020 18:45]

Report message to a moderator

Re: Looking for examples to split string into container [message #55311 is a reply to message #55309] Wed, 28 October 2020 19:07 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1091
Registered: August 2007
Senior Contributor
HEllo David,


Klugier already pointed to the right function and interface (Upp::Split).
Upp::Split has a plenty of variants, just check the API docs.


Here is conversion of C# -> to U++ code:

#include <Core/Core.h>

using namespace Upp;

CONSOLE_APP_MAIN
{
	String text = "one\ttwo three:four,five six seven";
	Cout() << text << "\n";
	Vector<String> words = Split(text, [](int c) { return c == ':' || c == '\t' || c == ' ' || c == ',' || c == '.' ? 1 : 0; });
	for(const auto& s : words)
		Cout() << s << "\n";
}



Best regards,
Oblivion


Re: Looking for examples to split string into container [message #55313 is a reply to message #55311] Thu, 29 October 2020 01:06 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Oblivion wrote on Wed, 28 October 2020 19:07
HEllo David,


Klugier already pointed to the right function and interface (Upp::Split).
Upp::Split has a plenty of variants, just check the API docs.


Here is conversion of C# -> to U++ code:

#include <Core/Core.h>

using namespace Upp;

CONSOLE_APP_MAIN
{
	String text = "one\ttwo three:four,five six seven";
	Cout() << text << "\n";
	Vector<String> words = Split(text, [](int c) { return c == ':' || c == '\t' || c == ' ' || c == ',' || c == '.' ? 1 : 0; });
	for(const auto& s : words)
		Cout() << s << "\n";
}



Best regards,
Oblivion


Variation just to demonstrate findarg:

	Vector<String> words = Split(text, [](int c) { return findarg(c, ':', '\t', ' ', ',', '.') >= 0; });
Previous Topic: Vector a user defined struct for XML persistency
Next Topic: Convert struct to string and reconstruct a struct from string
Goto Forum:
  


Current Time: Thu Mar 28 20:52:11 CET 2024

Total time taken to generate the page: 0.01066 seconds