1

I'm wondering how (and in which way it's best to do it) to split a string with a unknown number of spaces as separator in C++/CLI?

Edit: The problem is that the space number is unknown, so when I try to use the split method like this:

String^ line;
StreamReader^ SCR = gcnew StreamReader("input.txt");
while ((line = SCR->ReadLine()) != nullptr && line != nullptr)
{
     if (line->IndexOf(' ') != -1)
        for each (String^ SCS in line->Split(nullptr, 2))
        {
            //Load the lines...

        }
}

And this is a example how Input.txt look:

ThisISSomeTxt<space><space><space><tab>PartNumberTwo<space>PartNumber3

When I then try to run the program the first line that is loaded is "ThisISSomeTxt" the second line that is loaded is "" (nothing), the third line that is loaded is also "" (nothing), the fourth line is also "" nothing, the fifth line that is loaded is " PartNumberTwo" and the sixth line is PartNumber3.

I only want ThisISSomeTxt and PartNumberTwo to be loaded :? How can I do this?

HalfEvil
  • 113
  • 2
  • 10
  • See here : http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c The boost libraries will help greatly. – FreudianSlip Aug 12 '12 at 20:38
  • I'm not using boost :/ It's must be some way to do this with the .net framework? If it can be done in C# it's easy to convert to C++/CLI – HalfEvil Aug 12 '12 at 20:40
  • using managed c++? why is c++11 in the tags? – Gir Aug 12 '12 at 20:42

3 Answers3

1

Why not just using System::String::Split(..)?

Simon
  • 1,496
  • 8
  • 11
1

The following code example taken from http://msdn.microsoft.com/en-us/library/b873y76a(v=vs.80).aspx#Y0 , demonstrates how you can tokenize a string with the Split method.

using namespace System;
using namespace System::Collections;
int main()
{
   String^ words = "this is a list of words, with: a bit of punctuation.";
   array<Char>^chars = {' ',',','->',':'};
   array<String^>^split = words->Split( chars );
   IEnumerator^ myEnum = split->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      String^ s = safe_cast<String^>(myEnum->Current);
      if (  !s->Trim()->Equals( "" ) )
            Console::WriteLine( s );
   }
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
1

I think you can do what you need to do with the String.Split method.

First, I think you're expecting the 'count' parameter to work differently: You're passing in 2, and expecting the first and second results to be returned, and the third result to be thrown out. What it actually return is the first result, and the second & third results concatenated into one string. If all you want is ThisISSomeTxt and PartNumberTwo, you'll want to manually throw away results after the first 2.

As far as I can tell, you don't want any whitespace included in your return strings. If that's the case, I think this is what you want:

String^ line = "ThisISSomeTxt   \tPartNumberTwo PartNumber3";
array<String^>^ split = line->Split((array<String^>^)nullptr, StringSplitOptions::RemoveEmptyEntries);
for(int i = 0; i < split->Length && i < 2; i++)
{
    Debug::WriteLine("{0}: '{1}'", i, split[i]);
}

Results:

0: 'ThisISSomeTxt'
1: 'PartNumberTwo'
David Yaw
  • 27,383
  • 4
  • 60
  • 93