{"id":2014,"date":"2015-06-09T09:09:18","date_gmt":"2015-06-09T09:09:18","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2014"},"modified":"2016-02-01T13:45:07","modified_gmt":"2016-02-01T13:45:07","slug":"using-strings-searching-strings","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/strings\/string-class\/using-strings-searching-strings","title":{"rendered":"Using Strings-Searching Strings"},"content":{"rendered":"<h4>\nDoes String Contain<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tif (s1.find(s2) != std::string::npos)\r\n\t\tcout &lt;&lt; &quot;String found!&quot; &lt;&lt; endl;\r\n<\/code><\/pre>\n<h4>\nDo Strings Match<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tif (my_string1.compare(my_string2) == 0)\t\t\/\/0=strings are the same\r\n\t{\r\n\t\t\/\/Strings match\r\n\t}\r\n<\/code><\/pre>\n<h4>\nDoes String Start With<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tif (CommandResult.find(&quot;abc&quot;) == 0)\r\n<\/code><\/pre>\n<h4>\nFind first occurrence<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tstring s1 = &quot;hello world&quot;;\r\n\t\r\n\tif (s1.find(&#39; &#39;) != string::npos)\r\n\t{\r\n\t\tcout  &lt;&lt; &quot;Space found &quot; &lt;&lt; endl;\r\n\t}\r\n\telse\r\n\t{\r\n\t\tcout  &lt;&lt; &quot;No spaces found&quot; &lt;&lt; endl;\r\n\t}\r\n<\/code><\/pre>\n<p>\nIf you want to know where it was found:\n<\/p>\n<pre>\r\n<code>\r\n\tstring s1 = &quot;hello world&quot;;\r\n\tsize_t find_result;\r\n\t\t\r\n\tif ((find_result = s1.find(&#39; &#39;)) != string::npos)\r\n\t{\r\n\t\tcout  &lt;&lt; &quot;Space found at character &quot; &lt;&lt; find_result &lt;&lt; endl;\r\n\t}\r\n\telse\r\n\t{\r\n\t\tcout  &lt;&lt; &quot;No spaces found&quot; &lt;&lt; endl;\r\n\t}\r\n<\/code><\/pre>\n<h4>\nFind last occurance<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tstring s1 = &quot;hello 12&quot;;\r\n\tsize_t find_result;\r\n\t\t\r\n\tif ((find_result = s1.rfind(&#39; &#39;)) != string::npos)\t\/\/Searches from the end but still returns an index from the start of the string\r\n\t{\r\n\t\tcout  &lt;&lt; &quot;Space found at character &quot; &lt;&lt; find_result &lt;&lt; endl;\r\n\t}\r\n\telse\r\n\t{\r\n\t\tcout  &lt;&lt; &quot;No spaces found&quot; &lt;&lt; endl;\r\n\t}\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Does String Contain if (s1.find(s2) != std::string::npos) cout &lt;&lt; &quot;String found!&quot; &lt;&lt; endl; Do Strings Match if (my_string1.compare(my_string2) == 0) \/\/0=strings are the same { \/\/Strings match } Does String Start With if (CommandResult.find(&quot;abc&quot;) == 0) Find first occurrence string s1 = &quot;hello world&quot;; if (s1.find(&#39; &#39;) != string::npos) { cout &lt;&lt; &quot;Space found &quot; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[97],"tags":[],"class_list":["post-2014","post","type-post","status-publish","format-standard","hentry","category-string-class"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=2014"}],"version-history":[{"count":5,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2014\/revisions"}],"predecessor-version":[{"id":2502,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2014\/revisions\/2502"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}