Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Parsing a String in C

Posted on 2011-03-05
Medium Priority
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).


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

Does this make sense?
Question by:PGRBryant
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
LVL 32

Expert Comment

ID: 35045163
I never did a VS managed form before; so I can only offer you what I tried out after reviewing msdn pages.
   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

Open in new window


Expert Comment

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'...

Author Comment

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.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 32

Expert Comment

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.

Author Comment

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


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

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;
LVL 32

Accepted Solution

phoffric earned 2000 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];
         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.

Here is code to illustrate a similar String.Substring Method taken from:
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.)

Author Closing Comment

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.

LVL 32

Expert Comment

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.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

618 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