Using while with string.find()?

This is a discussion on Using while with string.find()? within the C++ Programming forums, part of the General Programming Boards category; I'm running a while loop that looks for a character and removes it from a string. Eventually, there won't BE ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    106

    Using while with string.find()?

    I'm running a while loop that looks for a character and removes it from a string. Eventually, there won't BE anymore of these characters in the string. However, I was wondering if I could test for some sort of value on, say, the find() method to see if the character was still in the string, and if it wasn't, cause the loop to break?

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Testing to see if there are any more of said characters will still require searching through the string, so doing so would cause you to be searching the string twice as much as you should be per iteration.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,339
    The find() method returns string::npos if it the search string is not found.

    You should use std::remove though if you want to remove all instances of a character from a string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string.find()
    By franse in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2008, 04:19 PM
  2. help with 'string.find'
    By barneygumble742 in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2005, 11:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21