473,516 Members | 3,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parsing a String in Visual C++

50 New Member
I want to parse out a string in Visual Studio .NET 2003 in C++. This is the format of the string:

T1 = "Hello"

It comes from a script file which I load into my gui app. It loads fine and I am currently using the split() function to get the word out. I have a problem using that because the way I have it setup, it only gets one word. So if it was

T1 = " Hello World"

then only Hello would be grabbed. Also I don't know why but there has to be a space in between the first quotation and Hello or else it will not work correctly. I thought of a different algorithm of acheiving the same solution.

Expand|Select|Wrap|Line Numbers
  1. int num, num1, num2;
  2. String * temp;
  3.  
  4. for (int x = 0; x <= line->Length; x++) 
  5. {    
  6.     line = line->Trim();
  7. }
  8. num1 = line->IndexOf("\"");
  9. num2 = line->LastIndexOf("\"");
  10. num = num2-num1;
  11.  
  12. temp = line->Substring(num1, num);
  13.  
  14. richTextBox2->Text = temp;
  15.  
  16.  
It compiles fine but as soon as I load the Script File, it gives me an error. The idea behind this code is that I trim the whole line first so there's no whitespaces at all. Then I find the position of the first and second quotation. Afterwards I subtract num2 by num1 to find out how many characters there are inside the quotations. Finally I read the substring starting from the first quotation going up to the number of characters in the quotations.

Here is the complete current code that does work, but only for one word inside quotations.

Expand|Select|Wrap|Line Numbers
  1.  
  2.                                                 scriptString = richTextBox1->Text;
  3.             scriptReader = new StringReader(scriptString);
  4.  
  5.             String* delimStr = S"=() :[] {}  \t \" ";
  6.             Char delimiter[] = delimStr->ToCharArray();
  7.             String* split[] = 0;
  8.             int nextChar = 0;
  9.             String* line = "";
  10.             String* str;
  11.             //String* str2;// = str1->Trim(delim->ToCharArray());
  12.             //str2 = str2->ToString(str1->Trim(delim->ToCharArray()));
  13.  
  14.             //iterate over the string using the StringReader, printing each line. 
  15.             while(nextChar >= 0)
  16.             {    
  17.                 //reading line by line    
  18.                 line = scriptReader->ReadLine();
  19.                 //making sure it read correctly
  20.                 line = line->TrimStart(NULL);
  21.                 for (int x = 0; x <= line->Length; x++) 
  22.                 {    
  23.                     //line = line->Trim(NULL);
  24.                     split = line->Split(delimiter, x);
  25.                     richTextBox1->AppendText(S"\ncount = {0, 2} .............."); 
  26.                     richTextBox1->AppendText(__box(x)->ToString());
  27.                     IEnumerator* myEnum = split->GetEnumerator();
  28.  
  29.                     while (myEnum->MoveNext()) 
  30.                     {    //line = line->TrimStart(NULL);
  31.                         String* s = __try_cast<String*>(myEnum->Current);
  32.                         richTextBox1->AppendText(S" ");
  33.                         richTextBox1->AppendText("Last s: ");
  34.                         richTextBox1->AppendText(s);
  35.  
  36.                         //if (myEnum->MoveNext()) //save the last s (which is the name)
  37.                         if(myEnum->MoveNext() && s != NULL)
  38.                         {
  39.                             str = s;
  40.                         }
  41.                     }
  42.                 }
  43.  
  44.  
  45.                 if(line->StartsWith("T1"))
  46.                 {
  47.                     top1->Text = str;
  48.                     defaultText(sender, e, top1);
  49.                 }
  50.  
  51.  
  52. //check if there's any more lines
  53.                 nextChar = scriptReader->Peek();
  54.  
  55.  
My goal is to read inside the quotations somehow and save it into a string. It doesn't matter to me which alternative I use.

Thanks in advance, in the meantime I am going to be trying to figure out this myself as well.
Aug 21 '07 #1
2 2319
JosAH
11,448 Recognized Expert MVP
I'll move your thread over to the .NET Forum where it belongs.

kind regards,

Jos
Aug 22 '07 #2
bmerlover
50 New Member
I posted the solution in my other thread about quotations in richTextBox but I'll post it here too because it's the same answer.

As long as you write the script file inside the program, then the quotations work. Here is the code that works.

Expand|Select|Wrap|Line Numbers
  1.  
  2.                         scriptString = richTextBox1->Text;
  3.             scriptReader = new StringReader(scriptString);
  4.  
  5.             String* delimStr = S" =\"):[]{}"; 
  6.             Char delimiter[] = delimStr->ToCharArray();
  7.             String* split[] = 0;
  8.             int nextChar = 0;
  9.             int whiteSpaceCount = 0;
  10.             String* line = "";
  11.             String* str;
  12.             String* space = S" ";
  13.  
  14.             //iterate over the string using the StringReader, printing each line. 
  15.             while(nextChar >= 0)
  16.             {    
  17.                 //reading line by line    
  18.                 line = scriptReader->ReadLine();
  19.                 //erase any whitespaces until you see the first character of the string
  20.                 line = line->TrimStart(NULL);
  21.  
  22.                 for (int x = 0; x <= line->Length; x++) 
  23.                 {
  24.                     split = line->Split(delimiter, x);                
  25.                     IEnumerator* myEnum = split->GetEnumerator();
  26.                     str = String::Join(space, split);
  27.  
  28.                     while (myEnum->MoveNext()) 
  29.                     {    
  30.                         String* s = __try_cast<String*>(myEnum->Current);
  31.                     }
  32.  
  33.                 }
  34.  
  35.                 str = str->Substring(line->IndexOf("\"") + 1); 
  36.  
  37.                 while(str->StartsWith(S" "))
  38.                 {
  39.                     whiteSpaceCount++;
  40.                     str = str->Substring(line->IndexOf("\"") + whiteSpaceCount);            
  41.                 }
  42.  
  43.                 whiteSpaceCount = 0; //reset the counter, this counter is for white spaces in between the first quotation and the first word inside the quotation just incase there is a space. 
  44.  
  45.  
  46.                 if(line->StartsWith("T1"))
  47.                 {
  48.                     top1->Text = str;
  49.                     defaultText(sender, e, top1);
  50.                 }
  51.  
  52.                 //check if there's any more lines
  53.                 nextChar = scriptReader->Peek();
  54.  
  55.  
  56.  
Aug 27 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
9425
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $ Last-Modified: $Date: 2003/10/28 19:48:44 $ Author: A.M. Kuchling <amk@amk.ca> Status: Draft Type: Standards Track
4
2106
by: almurph | last post by:
Hi everyone, Can you help me please? Say you have hashtable of about 500 key/value pairs. This hashtable has to parse a word. If the word matches the key the then said word must be replaced by the value. My question is though - would parsing occur faster if the word is input as a type string or a type stringbuilder? Do you see my point?...
18
2143
by: ILCSP | last post by:
Hi, I just started learning Visual Basic (VB.NET 03) and I need to do this small program that will read a text file we get from another company that has survey data, parse it and flatten it out and make single strings out of the records in it. The major difference between this space delimited file from the many examples in these groups is...
0
1128
by: Uncle Leo | last post by:
I created an OleDbDataAdapter with the wizard in Visual Studio 2003. It created a dataset, connectionstring etc. for me to work with. It also created a .xsd file where one of the columns type is set to date. My program is being used in many different countries, and many different local settings. Some time ago a user from Turkey contacted me...
1
1351
by: kellysgirl | last post by:
Im not good at parsing strings....and Ive been driving myslef nuts This is what I need to do....use an if/else statement to validate thata delimeter has been selected. These delimeters being comma, space and cr-lf. Parse text box contents----- Parse string to break out the words involving a loop and 2 pointer variable(old INdex and New...
2
4852
by: RG | last post by:
I am having trouble parsing the data I need from a Serial Port Buffer. I am sending info to a microcontroller that is being echoed back that I need to remove before I start the actual important data reading. For instance this is my buffer string: 012301234FFFFFFxFFFFFFxFFFFFFx Where the FFFFFF is my Hex data I need to read. I am using...
0
1977
by: =?Utf-8?B?VWxmIFRob3JzZW4=?= | last post by:
I use Visual Studio 2005 for a C-project using an external compiler, and came up with the idea that error parsing would be neat, i.e. enabling the functionality available for a "normal" build where you can double click a compile error/warning message and be guided to the file/line in question. Using sed to filter output from the compiler i...
5
8827
by: Mike C# | last post by:
Is there a decent cookie parser out there somewhere? I keep running into cookies that .NET can't handle. I've also found a couple of other parsers but they're choking as well.
1
4351
by: eyeore | last post by:
Hello everyone my String reverse code works but my professor wants me to use pop top push or Stack code and parsing code could you please teach me how to make this code work with pop top push or Stack code and parsing code my professor i does not like me using buffer reader on my code and my professor did even give me an example code for parsing...
0
7273
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7405
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7574
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7547
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5106
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3265
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.