Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing a TString

Status
Not open for further replies.

gnosis13

Programmer
Jul 13, 2001
263
US
How do I parse the text in a TString object? I have an edit box with a string in it like "1234 5678 wxyz". I would like to put 1234 in a variable, 5678 in a seperate variable, etc. This is easy to do in VB but BCB is killing me....
A+, N+, MCP
 
The problem is not with BCB but with how C++ handles strings. Off the top of my head I can think of two ways but neither is very eligent. One is to use the Pos() method. For example, if there are three spaces between each item in the string then
Code:
int SpacePlace = TString1->Strings.Pos("   ");
will return the first position of the three spaces. If it returns -1 then the 3 spaces do not exist.

You could then split the string into two string using something like SubString. (If you didn't know how many spaces are between each item, you could just search for one string, take the substring, then use Trim to clean up any leading and trailing spaces.)

You could repeat this procedure until you get a -1 indicating that there are no more items left.

Another way that I haven't tested is to send the TString to a stream and use the stream's ability to seperate variables using whitespace. TString has a method that will take it to a stream and back.

I just thought of a third way. If you convert the TString into a C++ string, you could use getline to split the string into parts. This would avoid splitting the string into parts. I use this alot. I don't know why I didn't think of it earlier. James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Dear,

I would suggest:
Determine how the fields are separated:
for instance
1234 5678 xywz
the spaces between 4 and 5, are they space, a tab are the distance of an exact lenght
wherein a value fits.
Those info are needed to accomplish the task.
Then use AnsiString methods to parse the Edit Box content.
 
The blocks of numbers always occur starting at the same position. Ex: the first block starts on [1] the second block on [20]. There is a variable length block of text at the end of each row always beginning at pos [50]. The space between blocks is just white space.

I am able to do a lot of manipulation using AnsiStrings. What I really need to do is pick off the data and store it as an AnsiString so I can reassemble it in a different order later on. I figure there is a way to do this with pointers but I am not familiar enough with BCB.

This works:
AnsiString tempString = "XXXX,";
tempString = memDisplayWindow->Lines->Strings[1];

This doesn't:
tempString[1] = memDisplayWindow->Lines->Strings[1];
"cannot convert 'char' to 'char*' error

This method wouldn't work on the variable length text anyway. Can I establish a pointer to a particular position, say position [20], then assign it to an AnsiString?

I will try your suggestions.....thanks.


A+, N+, MCP
 
I would just put the TString to an AnsiString then use SubString. Or, does
Code:
AnsiString tempString = memDisplayWindow->Lines->Strings.SubString(1,19);
work? James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
OK, the following code doesn't work:

AnsiString tempString, inkdesc;
tempString = "something at least 50 characterers long";
inkdesc = SubString (tempString[50], (StrLen (tempString)- 50)

These are the errors:

[C++ Error] Main.cpp(183): E2268 Call to undefined function 'SubString'
[C++ Error] Main.cpp(183): E2034 Cannot convert 'AnsiString' to 'const char *'
[C++ Error] Main.cpp(183): E2342 Type mismatch in parameter 'Str' (wanted 'const char *', got 'AnsiString')
[C++ Error] Main.cpp(183): E2121 Function call missing )

I think you cab see what I am after. I want everything in tempString copied to inkdesc begining with the 50th character. Everything else in my code works just fine.
A+, N+, MCP
 
Try
Code:
tempString.SubString(1, tempString.Length());
This will copy everything. You can adjust the beginning and ending index. James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Opps, got in a hurry . . .
Code:
inkdesc = tempString.SubString(1, tempString.Length());
James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Thanks a bunch.....everythings cookin'! A+, N+, MCP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top