Solved

Parsing a String in C

Posted on 2011-03-05
10
406 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 35045163
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
ID: 35045791
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
ID: 35062189
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:phoffric
ID: 35063479
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
 
LVL 1

Author Comment

by:PGRBryant
ID: 35066613
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 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 35066749
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
ID: 35066923
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
ID: 35287443
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
ID: 35288273
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 241
Getting the Error "User-defined type not defined" in MS Access 2013 16 82
Android development question 2 81
MySQL programmer starter 25 29
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

726 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