{"id":2478,"date":"2023-06-30T14:46:09","date_gmt":"2023-06-30T07:46:57","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=2478"},"modified":"2023-07-08T16:23:02","modified_gmt":"2023-07-08T09:23:02","slug":"csharp-string-methods","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-string-methods\/","title":{"rendered":"C# String Methods"},"content":{"rendered":"\n<p>This page introduces you to the most commonly used C# String methods, which allow you to manipulate strings effectively, including operations such as searching, replacing, formatting, and extracting substrings, among others.<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\">Section 1. Searching<\/h2>\n\n\n\n<p>In this section, you will learn various string methods that search for a substring in a string.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-contains\/\" data-type=\"page\" data-id=\"2480\">Contains()<\/a> &#8211; Returns true if the string contains the specified string, false otherwise.<\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-indexof\/\" data-type=\"page\" data-id=\"2490\">IndexOf()<\/a> &#8211; Returns the index of the first occurrence of the specified string in the string, or -1 if the string does not occur in the string.<\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-lastindexof\/\" data-type=\"page\" data-id=\"2498\">LastIndexOf()<\/a> &#8211; Returns the index of the specified string&#8217;s last occurrence, or -1 if the string does not occur in the string.<\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-startswith\/\">StartsWith()<\/a> &#8211; Returns true if the string starts with the specified value, false otherwise.<\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-endswith\/\">EndsWith()<\/a> &#8211; Returns true if the string ends with the specified value, false otherwise.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\">Section 2. Trimming<\/h2>\n\n\n\n<p>In this section, you&#8217;ll learn how to use various Trim* methods to remove the whitespace or a set of specified characters from a string:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-trim\/\">Trim()<\/a> &#8211; Returns a new string that is the same as the original string, but with any leading and trailing whitespace characters removed.<\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-trimstart\/\">TrimStart()<\/a> &#8211; Removes all whitespace characters at the beginning of the current string. <\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-trimend\/\">TrimEnd()<\/a> &#8211; Removes all whitespace characters at the end of the current string.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\">Section 3. Padding<\/h2>\n\n\n\n<p>In this section, you will learn how to pad a string with specified characters to achieve a desired length.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-padleft\/\">PadLeft()<\/a> &#8211; Returns a new string that is the specified length, with the specified character padded to the left.<\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-padright\/\">PadRight()<\/a> &#8211; Returns a new string that is the specified length, with the specified character padded to the right.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\">Section 4. Extracting<\/h2>\n\n\n\n<p>In this section, you will learn how to extract a portion of a string and split the string into an array of substrings:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-substring\/\">Substring()<\/a> &#8211; Returns a new string that is a substring of the original string, starting at the specified index and with the specified length.<\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-split\/\">Split()<\/a> &#8211; Returns an array of strings that are the result of splitting the string at each occurrence of the specified separator string.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\">Section 5. Replacing &amp; Inserting<\/h2>\n\n\n\n<p>In this section, you&#8217;ll learn how to replace a substring in a string with a new one:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-replace\/\">Replace<\/a> &#8211; Returns a new string with all occurrences of the specified old value replaced with the specified new value.<\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-insert\/\">Insert()<\/a> &#8211; Returns a new string with a string inserted at a specified index of the current string.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\">Section 6. Changing Cases<\/h2>\n\n\n\n<p>In this section, you&#8217;ll learn how to convert a string to lowercase or uppercase:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-tolower\/\">ToLower()<\/a> &#8211; Returns a lowercase version of the string as a new copy.<\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-toupper\/\">ToUpper()<\/a> &#8211; Returns an uppercase version of the string as a new copy.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\">Section 7. Concatenating<\/h2>\n\n\n\n<p>In this section, you&#8217;ll learn how to use the Join() and Concat() methods to concatenate two or more strings into a single string:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-join\/\">Join()<\/a> &#8211; concatenate two or more strings in a collection into a string with a specified separator.<\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-string-methods\/csharp-string-concat\/\">Concat()<\/a> &#8211; concatenate two more strings into a string.<\/li>\n<\/ul>\n<\/div><\/div>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2478\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-string-methods\/\"\n\t\t\t\tdata-post-title=\"C# String Methods\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2478\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-string-methods\/\"\n\t\t\t\tdata-post-title=\"C# String Methods\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Use C# String methods to manipulate strings effectively, including searching, replacing, formatting, and extracting substrings, among others.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":8,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2478","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/2478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/comments?post=2478"}],"version-history":[{"count":5,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/2478\/revisions"}],"predecessor-version":[{"id":2609,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/2478\/revisions\/2609"}],"wp:attachment":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/media?parent=2478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}