Solved

Parsing a String in C

Posted on 2011-03-05
10
392 Views
Last Modified: 2012-06-27
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
Comment
Question by:PGRBryant
10 Comments
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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
 
LVL 3

Expert Comment

by:gmleeman
Comment Utility
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
 
LVL 1

Author Comment

by:PGRBryant
Comment Utility
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
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Comment

by:PGRBryant
Comment Utility
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
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
Comment Utility
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
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
Comment Utility
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
 
LVL 1

Author Closing Comment

by:PGRBryant
Comment Utility
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
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
An API detour question 7 64
Specific format 21 139
python sqlite question 11 42
Not needed 13 52
I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now