开发者

How to find the location of two consecutive spaces in a string

开发者 https://www.devze.com 2022-12-21 14:08 出处:网络
I want to che开发者_开发技巧ck if my string has two consecutive spaces in it.What\'s the easiest way to find out?Use the find() method of std::string. It returns the special constant std::string::npos

I want to che开发者_开发技巧ck if my string has two consecutive spaces in it. What's the easiest way to find out?


Use the find() method of std::string. It returns the special constant std::string::npos if the value was not found, so this is easy to check for:

if (myString.find("  ") != std::string::npos)
{
  cerr << "double spaces found!";
}


#include <string>

bool are_there_two_spaces(const std::string& s) {
    if (s.find("  ") != std::string::npos) {
        return true;
    } else {
        return false;
    }
}


This one of Jon Skeet's fave topics: see this presentation


Make search for "  " in the string.


Using C:

#include <cstring>
...
addr = strstr (str, "  ");
...


string s = "foo  bar";
int i = s.find("  ");
if(i != string::npos)
   cout << "Found at: " << i << endl;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号