GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Oct-2006, 18:28
AndrewBucklin AndrewBucklin is offline
New Member
 
Join Date: Oct 2006
Posts: 3
AndrewBucklin is on a distinguished road

How to remove comma from string?


I can't figure it out! I have a string, with a comma somewhere in the middle of it. How can I remove the comma?
  #2  
Old 05-Oct-2006, 19:27
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: How to remove comma from string?


Can you post what you have tried?
Also what kind of string are you using?
CPP / C++ / C Code:
char var1[20];
or
CPP / C++ / C Code:
#include <string>
string var1;
  #3  
Old 05-Oct-2006, 19:29
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to remove comma from string?


Quote:
Originally Posted by AndrewBucklin
I can't figure it out! I have a string, with a comma somewhere in the middle of it. How can I remove the comma?

With pencil and paper (remember those?) you would erase it. WIth strings, you erase it:

CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{

    string str("Can you find, the spurious comma?");

    cout << "Original string: <" << str << ">" << endl;
    string::size_type i = str.find(",", 0);
    if (i != string::npos) {
        cout << "i = " << i << endl;
        str.erase(i, 1);
        cout << "After erase:     <" << str << ">" << endl;
    }
    else {
        cout << "Couldn't find the comma. Oh, well..." << endl;
    }
    return 0;
}

Output:

Code:
Original string: <Can you find, the spurious comma?> i = 12 After erase: <Can you find the spurious comma?>

Regards,

Dave
  #4  
Old 05-Oct-2006, 21:07
AndrewBucklin AndrewBucklin is offline
New Member
 
Join Date: Oct 2006
Posts: 3
AndrewBucklin is on a distinguished road

Re: How to remove comma from string?


Sweet. I'm going to try that. I'm a NOOB, so I'm also going to try to understand exactly what all that means too, rather than just copying it. Thanks guys... you saved the day...
  #5  
Old 05-Oct-2006, 21:10
AndrewBucklin AndrewBucklin is offline
New Member
 
Join Date: Oct 2006
Posts: 3
AndrewBucklin is on a distinguished road

Re: How to remove comma from string?


That totally worked! Thanks so much!!!
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a .html file, check that file for links salemite C Programming Language 10 17-Jan-2008 07:56
variables return to previous value after i try to set them nasaiya MS Visual C++ / MFC Forum 2 14-Jun-2005 00:43
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 08:14
Converting string to float CT++ C++ Forum 2 10-May-2005 11:29
Removing the last comma in a string Kurtanator MySQL / PHP Forum 2 10-Mar-2004 14:58

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:13.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.