Reading numbers form a CString and adding them

Posted on 2004-11-10
Last Modified: 2013-11-20
I have a CString, say:

CString testString = "abc123";

I want to add the 123, add convert the new number to a string.

I don't need to search the string as I know the positons of the digits I want to add (in this case positions 3,4, and 5..... 0 based)

thanks for any help!
Question by:galneweinhaw
    LVL 86

    Expert Comment

    You could use

    int n = 123;
    CString testString = "abc";
    CString numString = testString.Right(testString.Length() - 3); // get a string onlys containing the numbers
    testString = testString.Left(3); // cut off the old numbers

    int tmp = atoi((LPCTSTR)numString);

    numString.Format("%d", n + tmp);

    testString = testString + numString; // put the parts together again

    Author Comment

    Thanks, but I don't think I was very clear on what I need to do, I'll try to clarify:

    I have a CString "@@@@12@@@"  where 1 and 2 are the two numbers I want to add and @ may be #'s or letters.

    I want to do 1 + 2 = 3

    then convert the 3 to a CString.

    the numbers will always be single digit (0-9)

    thanks again.

    LVL 86

    Accepted Solution

    Aaah, OK, then it is

    CString testString = "abc123";
    CString strNum;
    int n1 = testString[3] - '0'; // subtracting ASCII '0' will give the numeric value
    int n2 = testString[4] - '0';

    strNum.Format("%d", n1 + n2);


    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    how do i create updater to My Activex application? 3 71
    format the code in java 6 60
    MaxSpan challenge 9 50
    wordappend challenge 8 45
    Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
    If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    761 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

    8 Experts available now in Live!

    Get 1:1 Help Now