Solved

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

Posted on 2006-06-15
8
388 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

744 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

13 Experts available now in Live!

Get 1:1 Help Now