Re: Easy way to read out formated string ?
"Polaris" <etpolaris@hotmail.com> wrote:
I have a string which will always be in the format below:
"<string1><string2><string3>"
Any of string1, string2 and string3 could be null, and their lengths is not
known.
Just wondering, if there is an easy way to parse the whole string and read
out string1, string2 and string3 into 3 strings?
What environment? ATL's string class can do this.
C:\tmp>type x.cpp
#include <stdio.h>
#include <atlstr.h>
int main()
{
CAtlString s = "<string1><string2><string3>";
int iStart = 0;
for(
CAtlString s1 = s.Tokenize("<>",iStart);
iStart >= 0;
s1 = s.Tokenize("<>",iStart)
)
{
printf( "%s\n", s1 );
}
return 0;
}
C:\tmp>cl x.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for
80x86
Copyright (C) Microsoft Corporation. All rights reserved.
x.cpp
Microsoft (R) Incremental Linker Version 8.00.50727.42
Copyright (C) Microsoft Corporation. All rights reserved.
/out:x.exe
x.obj
C:\tmp>.\x
string1
string2
string3
C:\tmp>
Although, now that I think about it, if one of the strings is null, it will
just skip it. If you need to know about them, you could tokenize on ">"
alone, and just delete the first character of each string.
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.