{"id":105,"date":"2026-05-20T11:43:28","date_gmt":"2026-05-20T11:43:28","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=105"},"modified":"2026-05-22T10:32:43","modified_gmt":"2026-05-22T10:32:43","slug":"common-string-functions","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=common-string-functions","title":{"rendered":"Common String Functions"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">C++ provides many built-in string functions that make it easy to work with text data. These functions help in finding length, combining strings, searching text, extracting characters, and more.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are String Functions?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">String functions are predefined functions used to perform operations on strings quickly and efficiently.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use String Functions?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">String functions are useful because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Simplify text processing<\/li>\n\n\n\n<li>Reduce code complexity<\/li>\n\n\n\n<li>Save programming time<\/li>\n\n\n\n<li>Improve code readability<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">String Library<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To use string functions, include:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;string&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Common String Functions in C++<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. length()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Returns the number of characters in a string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br>using namespace std;<br><br>int main() {<br><br>    string text = \"Programming\";<br><br>    cout &lt;&lt; text.length();<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>11<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. size()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Works similar to <code>length()<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cout &lt;&lt; text.size();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. append()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Adds one string to another.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br>using namespace std;<br><br>int main() {<br><br>    string a = \"Hello \";<br>    string b = \"World\";<br><br>    a.append(b);<br><br>    cout &lt;&lt; a;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello World<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. + Operator<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Joins two strings.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string full = \"C++ \" + string(\"Language\");<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. substr()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Extracts part of a string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br>using namespace std;<br><br>int main() {<br><br>    string text = \"Programming\";<br><br>    cout &lt;&lt; text.substr(0, 6);<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Progra<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. find()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Searches for text inside a string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br>using namespace std;<br><br>int main() {<br><br>    string text = \"Hello World\";<br><br>    cout &lt;&lt; text.find(\"World\");<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>6<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. at()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Accesses a specific character safely.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string text = \"Cplusplus\";<br><br>cout &lt;&lt; text.at(2);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">8. empty()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Checks if a string is empty.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string text = \"\";<br><br>cout &lt;&lt; text.empty();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">9. clear()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Removes all characters from a string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string text = \"Hello\";<br><br>text.clear();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">10. compare()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Compares two strings.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br>using namespace std;<br><br>int main() {<br><br>    string a = \"Ali\";<br>    string b = \"Ali\";<br><br>    if (a.compare(b) == 0) {<br>        cout &lt;&lt; \"Equal\";<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Equal<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">11. erase()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Removes characters from a string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string text = \"Programming\";<br><br>text.erase(0, 3);<br><br>cout &lt;&lt; text;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>gramming<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">12. insert()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Inserts text into a string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string text = \"Hello\";<br><br>text.insert(5, \" World\");<br><br>cout &lt;&lt; text;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello World<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of String Functions<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Easy text manipulation<\/li>\n\n\n\n<li>Faster development<\/li>\n\n\n\n<li>Cleaner code<\/li>\n\n\n\n<li>Built-in optimization<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why String Functions are Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">String functions are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Simplify string operations<\/li>\n\n\n\n<li>Improve code efficiency<\/li>\n\n\n\n<li>Are widely used in applications<\/li>\n\n\n\n<li>Help manage text data easily<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of editing text in a document:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add text<\/li>\n\n\n\n<li>Remove text<\/li>\n\n\n\n<li>Search words<\/li>\n\n\n\n<li>Compare sentences<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">These tasks are performed using string functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Common string functions in C++ make string handling simple and efficient. They help programmers perform text operations quickly using built-in tools and are essential for modern programming applications.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate C++ > Arrays and Strings > Common String Functions<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779445860482\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"menu_order":29,"template":"","class_list":["post-105","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Common String Functions - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ common string functions like length, append, substr, find, erase, insert, and compare for efficient text handling.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Common String Functions - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ common string functions like length, append, substr, find, erase, insert, and compare for efficient text handling.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-22T10:32:43+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=common-string-functions\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Common String Functions - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-20T11:43:28+00:00\",\"dateModified\":\"2026-05-22T10:32:43+00:00\",\"description\":\"Learn C++ common string functions like length, append, substr, find, erase, insert, and compare for efficient text handling.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate C++ > Arrays and Strings > Common String Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Common String Functions - Learn C++Language with GiGz.PK","description":"Learn C++ common string functions like length, append, substr, find, erase, insert, and compare for efficient text handling.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Common String Functions - Learn C++Language with GiGz.PK","og_description":"Learn C++ common string functions like length, append, substr, find, erase, insert, and compare for efficient text handling.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T10:32:43+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=common-string-functions","url":"https:\/\/gigz.pk\/","name":"Common String Functions - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-20T11:43:28+00:00","dateModified":"2026-05-22T10:32:43+00:00","description":"Learn C++ common string functions like length, append, substr, find, erase, insert, and compare for efficient text handling.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Intermediate C++ > Arrays and Strings > Common String Functions"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}