Thread: Using while with string.find()?

  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
    New York
    Posts
    1,905
    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,366
    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, 05:19 PM
  2. help with 'string.find'
    By barneygumble742 in forum C++ Programming
    Replies: 10
    Last Post: 12-24-2005, 12:28 AM