Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-06-15
8
Medium Priority
?
435 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
Industry Leaders: 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

824 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