{"id":2011,"date":"2015-06-09T08:56:46","date_gmt":"2015-06-09T08:56:46","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2011"},"modified":"2020-07-30T11:22:37","modified_gmt":"2020-07-30T11:22:37","slug":"using-strings-basics","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/strings\/string-class\/using-strings-basics","title":{"rendered":".Using Strings-Basics"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#include &lt;string>\n\/\/#include &lt;string.h>   \/\/(.h is used for C not C++)\n#include &lt;cstring>\nusing namespace std;   \/\/You need this or you need to use std::string when declaring\n#include &lt;sstream>    \/\/Lets you create strings using &lt;&lt; for instance<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Creating and using strings<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tstring result;\n\tstring s1 = \"hello \";\n\tstring s2 = \"world\";\n\tresult = s1 + s2;\n\n\t\/\/or you could do this:\n\tresult = s1;\n\tresult += \" \";\n\tresult += s2;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">If you get errors?<\/h5>\n\n\n\n<p>You may be defining the strings somewhere not allowed so instead try defining the strings separately at the top of the function, e.g.:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tstring result;\n\tstring s1 ;\n\tstring s2 ;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\t...\n\ts1 = \"hello \";\n\ts2 = \"world\";\n\tresult = s1 + s2;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Does string equal<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tif (MyString == \"ab cd\")\n\t{\n\t}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">switch (MyString) ?<\/h5>\n\n\n\n<p>No, you can&#8217;t use switch() on a string in c++<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Passing String to a function that expects char *<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tstring s1;\n\tmy_function(s1.c_str());<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">String Length<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tint length = my_string.length();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Delete&nbsp;String<\/h4>\n\n\n\n<p>You don&#8217;t have to. When the string goes out of scope, it&#8217;s destructor will be called automatically and the memory will be freed. If you want to clear the string right now (without waiting until it goes out of scope) just use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mystringname.clear().<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Is String is set or not(null)?<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tif (my_string.empty())\n\t\t\/\/ nothing in my_string<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Get Section Of A String<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tsome_other_string = my_string.substr(8, string::npos);    \/\/Copy from character 8 to the end<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Creating and using strings If you get errors? You may be defining the strings somewhere not allowed so instead try defining the strings separately at the top of the function, e.g.: Does string equal switch (MyString) ? No, you can&#8217;t use switch() on a string in c++ Passing String to a function that expects char [&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-2011","post","type-post","status-publish","format-standard","hentry","category-string-class"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2011","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=2011"}],"version-history":[{"count":23,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2011\/revisions"}],"predecessor-version":[{"id":3395,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2011\/revisions\/3395"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}