• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

Parsing a String in C

I'm hoping this is going to be really simple, but for some reason I can't seem to make it work.

I'm working in Visual Studio 2010 Express, and I'm trying to write a really simple C++ form application.

Basically, I want the user to input a string into a text box, and then I'm going to split up that string that the user enters and display parts of it in various other text boxes on the form.

To be more specific, let's say that the user inputs "Hello, World!" into the text box and clicks OK (the clicking is what will initialize my data parsing).

I know how to access the ENTIRE string of "Hello, World!" very very easily, it's just like this:

(Note: I've labeled the text box txtbxMainText).

txtbxMainText->Text

Now, how do I pull out just "World" or just "or" or any other part of that string easily?

Does this make sense?
0
PGRBryant
Asked:
PGRBryant
1 Solution
 
phoffricCommented:
I never did a VS managed form before; so I can only offer you what I tried out after reviewing msdn pages.
    http://msdn.microsoft.com/en-us/library/system.string.split(VS.71).aspx
   array<Char>^chars = {' ',  ',',  ':',  '.', '-', '|'};
   array<String^>^words = words2->Split( chars );

Open in new window

My form also had a Next Word text box, and every time I hit the OK button, the next word appeared in it. The next word would rotate around after the last word showed up by using a modulo index of
 words->Length

Open in new window

0
 
gmleemanCommented:
if you were to use pure C, it would look quite different...Do you want to use C or C++ and all the modern MSVS methods?  Just curious because your Question Title uses 'C'...
0
 
PGRBryantAuthor Commented:
In this case I meant C++, my apologies. Since I'm working with the forms in Visual Express 2010.

Working with strings in C wasn't difficult to figure out, in my experience.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
phoffricCommented:
Is the solution I provided what you were looking for?

Understand that when you are using Visual Studio 2010 Express to write a C++ forms application, then the code produced is not totally in the ANSI approved ISO/IEC 14882 C++ standard.

For that reason, I had to research this variant C++ language to come up with a working form. This variant C++ language provided the split function.
0
 
PGRBryantAuthor Commented:
Perhaps I don't understand what you did.

I have a really, essentially random, first piece of text that I recieve, and I"m trying to parse that data in forms.

I figured out how to do it with just simple C formatting, but I was hoping to be able to make it look more eloquent with a form-based approach.

The initial string is that is entered into the main text box is something like

%TRORANGE^WORLD$HAPPY$DAYS^17790301=4443950023

And I'm going to be doing things like pulling out the "ORANGE" and the "HAPPY", etc, and I need to individually pull those out... for example, in more simple C I could pull out the "TR" by just
mystr.substr(1,2);
0
 
Subrat (C++ windows/Linux)Software EngineerCommented:
This may give you some idea.

#include "iostream"
#include "sstream"

int main(){
  std::string s("Somewhere down the road"),s1,s2,s3,s4;
  std::stringstream ss(s);
  ss >> s1 >> s2 >> s3 >> s4;
  std::cout << "Substrings: " << s1 << "*" << s2 << "*" << s3 <<"*"<< s4 << std::endl;
  return 0;
}
0
 
phoffricCommented:
From your OP, I thought you were just trying to find the words entered into a text box which were delimited by a space (or other delimiters of your choosing). Here is the OK click method that I used:
   static int iNext=0;
   private: System::Void OK_Click(System::Object^  sender, System::EventArgs^  e) {
         String^ words2 = enterWordsBox->Text;
         int idx;
         array<Char>^chars = {' ',  ',',  ':',  '.', '-', '|'};
         array<String^>^words = words2->Split( chars );
         int modulus = words->Length;

         if ( modulus > 0 ) {
            while (1) {
               idx = iNext++ % modulus;
               if( words[idx]->Length > 0 ) {
                  nextWordTextBox->Text = words[idx];
                  break;
               }
            }
         }
         else {
            nextWordTextBox->Text = " ** PLEASE ENTER SOMETHING ** ";
         }
      }

Open in new window


>> for example, in more simple C I could pull out the "TR" by just:  mystr.substr(1,2);   I guess you meant simple C++ where substr is a method in the string class.
       http://www.cplusplus.com/reference/string/string/substr/

Here is code to illustrate a similar String.Substring Method taken from:
    http://msdn.microsoft.com/en-us/library/system.string.substring(v=VS.71).aspx
String *myString = L"abc";
bool test1 = String::Compare(myString->Substring(2,1), L"c") == 0;  // This is true.
myString->Substring(3,1);  // This throws ArgumentOutOfRangeException.
bool test2 = String::Compare(myString->Substring(3,0), String::Empty) == 0;  // This is true.

Open in new window

Again, this last bit of code uses libraries which are neither in standard C nor C++ , but useful if you want to use VS 2010 Express forms. (I don't have experience with this language, so I cannot say whether it is used in other systems.)
0
 
PGRBryantAuthor Commented:
I apologize pho, you definitely put in the effort and deserve the points, I got distracted with other projects and didn't come back to verify your work.

Thanks!
0
 
phoffricCommented:
No apologies necessary. But considerate of you to do so. I understand that busy people are, well, busy. I figured you would get around to this when you had time.

But just to clarify, you don't have to accept a post if you have questions about it remaining. What counts is your satisfaction, and not the effort we put into it. So, if you have questions about anything related to this question, please do not hesitate to ask here.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now