Solved

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

Posted on 2006-06-15
8
407 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rspiege1
ID: 16916352
it could nto find snscanf.  what should i include to use this?
0
 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

752 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