Solved

how can I parse for a piece of a string in c++

Posted on 2006-06-15
8
389 Views
Last Modified: 2010-10-05
I have the following string and would like to know how I can parse to get just the first part of the string.

temp string = "C0OH5Z14=017=";


In the example above i am looking to get the "C0OH5Z" portion of the string.  My only problem is that this part of the string may be larger or smaller.  It will however though always have some kind of wierd character at the end of it.  Not numerical or alphanumeric.  It will be either the box shown above or an = sign.  So I figure there has got to be a way to grab everything right up until that character.

Any help would be apprecaited.

thanks,
Rob
0
Comment
Question by:rspiege1
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 16916181
locale loc;
int n;

while(isalnum(temp[i],loc)) n++;

string result = temp.substr(0,n);

which will extract all alphanumeric character until the "box".
0
 

Author Comment

by:rspiege1
ID: 16916241
the the code above, what is I?  Should i be cyclign through the string.  Also it is complaining that isalnum only takes one argument.  I am going to check into isalnum.  I think that may be all i need.
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16916322
Hi rspiege1,

Try:

char firstPart[42];
snscanf ( string, sizeof(firstPart, "%[^=]", firstPart );

Paul
0
 

Author Comment

by:rspiege1
ID: 16916352
it could nto find snscanf.  what should i include to use this?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Accepted Solution

by:
PaulCaswell earned 300 total points
ID: 16916765
Ok,

Try:

sscanf ( string, "%[^=]", firstPart );

But be sure the 'firstPart' array will always be longer that the maximum size of the first part of the string.

Paul
0
 
LVL 86

Expert Comment

by:jkr
ID: 16917158
>>the the code above, what is I?  Should i be cyclign through the string.

Well, just make that

string GetValueFromString(string& in) {

locale loc;
int n;

while(isalnum(in[i],loc)) n++;

string result = in.substr(0,n);

return result;
}

and call that like

string val = GetValueFromString(temp);
0
 

Author Comment

by:rspiege1
ID: 16920090
thanks experts.  I ended up using the sscanf method.  it worked great.  Only thing was I had to convert my string to a char*, but that did not affect the result.

thanks again.
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16923399
Thanks for the points!

Please remember that using scanf in C++ sould be kept to a minimum for many reasons.

a) scanf has no buffer-overflow checks in it.
b) There are clearer methods of doing this sort of thing.
c) It is not a good idea to significantly change the level of your system interface unnecessarily. scanf is commonly considered quite 'low' level nowadays.

But its a great tool when used responsibly.

Paul
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now