?
Solved

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

Posted on 2006-06-15
8
Medium Priority
?
425 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
Technology Partners: 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 1200 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

719 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