Replacing parts of a string


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Replacing parts of a string

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Posts
    173

    Replacing parts of a string

    I want to replace all -99999 in a line with just spaces.
    I've got it working with the following code....but is there a more elegant way of doing it?

    Code:
        
    string entireLine = name+strID+strCP+strX1+strX2+strX3+strCD+strPS+strSEID;
    string::size_type idx = 0;
    while (true)
    {
        idx = entireLine.find("-99999", idx);
        if (idx == string::npos)
             break;
        entireLine.replace(idx, 6,"      ");
        idx += 6;     
    }

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    I think this code gets the job done faily efficiently and in in a non-terribly versbose manner. You could use a few advanced features such as predicated and transform() but I don't think they're really worth the effort in this case. One thing I might do differently: I would first scan the string for all positions of 999999, store the positions in a vector or something and then apply replace in a sequenc, using the previously stored indexes but that's up to you.
    Danny Kalev

Similar Threads

  1. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 02:32 PM
  2. Replies: 1
    Last Post: 06-05-2001, 06:12 AM
  3. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 2
    Last Post: 08-11-2000, 12:11 PM
  4. Replies: 0
    Last Post: 06-22-2000, 07:30 AM
  5. Database problems
    By Robert Rieth in forum VB Classic
    Replies: 1
    Last Post: 04-11-2000, 03:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center