?
Solved

How can I edit this  string in C++?

Posted on 2010-01-12
6
Medium Priority
?
399 Views
Last Modified: 2013-12-14
Hi,

I have some data in a string.. and I need to extract pieces of it.

The string has this info in it

"The cost: $150 (Total)"

I need to get 150 out of the string.. I dont need any of the other info.. how can I do this?

Also.. originally the data is in a char*, so if we can do it somehow with a char* that works too

Thanks

0
Comment
Question by:bail3yz
[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
6 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 26300061
Several thoughts come to mind.  If it really is a char* type (not an ANSI string), sscanf should do nicely.

int Value;

  if (sscanf ("The cost: $150 (Total)", "[^0-9]%d", &Value) > 0)
    // Value was extracted.


Good Luck,
Kent
0
 
LVL 40

Accepted Solution

by:
evilrix earned 2000 total points
ID: 26300070
The very simplest was if just to assign the string to a std::string and then use find_first_of and find_last_of to find that start and end index of the number.
http://www.cplusplus.com/reference/string/string/find_first_of/
http://www.cplusplus.com/reference/string/string/find_last_of/

Alternatively, if you want to keep it as a char * you can use strtol to extract the number. You will still need to locate the start thought, which you can do by enumerating each letter and use isdigit to check for a digit.
http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/
http://www.cplusplus.com/reference/clibrary/cctype/isdigit/

0
 
LVL 40

Expert Comment

by:evilrix
ID: 26300083
>> find that start and end index of the number
Heh, I forgot to point out you then use substr and stringstream to convert the string representation to a integer one =/
http://www.cplusplus.com/reference/string/string/substr/
http://www.cplusplus.com/reference/iostream/stringstream/
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:bail3yz
ID: 26300229
Hi evilrix,

I actually ended up using just find (which I didnt know about until your post!) and substr.. because the # is always different but the rest of the text is always the same..

Thanks for the help!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 26301895
If the rest of the string is always the same (guaranteed), you can simply do :

        const char* str = "The cost: $150 (Total)";
        int cost = atoi(str + strlen("The cost: $"));
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 26301902
Or if you want to avoid the strlen call :

        int cost = atoi(str + 11));
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

770 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