Click here to Skip to main content
16,004,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working in windows forms and am a bit lost with reading a string.

I have a text file which is read into a string:
String ^ TextString = this->textBox1->Text;

Now I want to read this string one number at a time. It should be filled with an undefined amount of integers. I want to select each input at a time, seperated by either a space or comma and
a) check whether it is an integer or not
b) if it is an int then do some stuff to it via sending it off to function store: store( x );

1. Is it possible to do this all in visual c++ or do i need to convert to std string? (I dont really want to mix in the same file)

2. If I have to use std, is this right? :

C++
String ^ TextString = this->textBox1->Text;  // string found


int_separator<int> sep(", "); // defines the seperation possibilities
tokenizer< int_separator<int> > tokens(TextString, sep); // tokenizes the string
BOOST_FOREACH (const string& num, tokens) // for each string value send into store function
{
   store( num );
}


Do you see a 'cleaner' way, without mixing standard c++ with visual? I'd very much appreciate to hear your thoughts. Thank you for reading, and for any responses.
Posted
Comments
[no name] 5-Mar-13 16:03pm    
http://msdn.microsoft.com/en-us/library/ms177202%28v=vs.100%29.aspx
lostandconfused1234 5-Mar-13 16:16pm    
it doesn't say how to find the string size. is there a function for this?
[no name] 5-Mar-13 18:27pm    
If you are using for each why do you need the string size? And yes there is a function for that.
Sergey Alexandrovich Kryukov 5-Mar-13 16:08pm    
Do you mean System.Windows.Forms with C++/CLI, or something else? MFC?
—SA
lostandconfused1234 5-Mar-13 16:17pm    
I was told the windows.forms created MFC. Is this wrong?

1 solution

I found I could more easily use this, modified to my string:

C++
std::string token, text("Here:is:some:text"); 
std::istringstream iss(text); // streams the string text
while ( getline(iss, token, ':') ) // gets the line, seperates into "tokens"
{
    store(token);
}
{
					
}


But then I need to somehow convert my String^ TextString to a std::string and replace this with the examples "text" string. Any ideas how?

***************************************************************************
To any who have the same problem:

http://www.daniweb.com/software-development/cpp/threads/27905/using-stringstream-for-tokenizing-a-string[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900