I want to check if there are two consecutive spaces in my line. What is the easiest way to find out?
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:
find()
std::string
std::string::npos
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 contains 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;
Source: https://habr.com/ru/post/1734548/More articles:How to start using DotNetOpenAuth - c #Unable to update text in UILabel - iphoneMoving Windows Mobile Screen - c #ELF feelings and dynamic linking in ARM linux - linuxLinq to sql "Root level data invalid" error for xml null column - c #how to get python to return floating point? - pythonConverting a WPF Project to Silverlight - wpfSyntax for using an array of wires as input - arraysjavascript/IE 6/7: проверьте, есть ли внутри iframe - javascriptWhere is the call in std :: map :: operator [] in this code? - c ++All Articles