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

I want to check if there are two consecutive spaces in my line. What is the easiest way to find out?

+3
source share
6 answers

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

if (myString.find("  ") != std::string::npos)
{
  cerr << "double spaces found!";
}
+6
source
#include <string>

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

This one of Jon Skeet contains topics: see this presentation

+1
source
Make search for "  " in the string.
0
source

Using C:

#include <cstring>
...
addr = strstr (str, "  ");
...
0
source
string s = "foo  bar";
int i = s.find("  ");
if(i != string::npos)
   cout << "Found at: " << i << endl;
0
source

Source: https://habr.com/ru/post/1734548/


All Articles