{"id":12493,"date":"2020-02-24T13:24:16","date_gmt":"2020-02-24T21:24:16","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=12493"},"modified":"2023-12-01T02:30:25","modified_gmt":"2023-12-01T10:30:25","slug":"ruby-string-methods","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/","title":{"rendered":"Ruby String Methods"},"content":{"rendered":"\n<p>Manipulating text is a common operation in many programming languages, and Ruby is no exception. For example, you may want to capitalize someone\u2019s name before it is printed on an invoice, or you may want to replace someone\u2019s apartment number if they have moved.<\/p>\n\n\n\n<p>In this guide, we break down the most useful string methods in Ruby. These include: finding out the length of a string, splitting strings into substrings, changing the case of a string, and finding and replacing text. By the end of this tutorial, you\u2019ll be a master at working with Ruby strings.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Accessing String Characters<\/strong><\/h2>\n\n\n\n<p>Strings are a data type that can contain letters, numbers, symbols, and whitespace characters. Here\u2019s an example of a string in Ruby:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>our_string = \"I am a Ruby string!\"<\/pre><\/div>\n\n\n\n<p>Each item in our string, like an array, has its own index number, starting with 0. We can use these index numbers to access individual items from our string. Here is a breakdown of the index values of the word <code>Career<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-table course-info-table\"><table><tbody><tr><td>C<\/td><td>a<\/td><td>r<\/td><td>e<\/td><td>e<\/td><td>r<\/td><\/tr><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><td>5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Right now, though, our string is stored in one variable, as one value. But what if we wanted to get a few characters from our string? That\u2019s where the <code>slice()<\/code> method comes in.<\/p>\n\n\n\n<p> The <code>slice()<\/code> method allows us to retrieve a single character or a range of characters from our string. Here\u2019s an example of <code>slice()<\/code> in action:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"Career\".slice(0)<\/pre><\/div>\n\n\n\n<p> Our code returns the following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>C<\/pre><\/div>\n\n\n\n<p>If we pass a single value, we\u2019ll get the character at that index position in the string. If we want to get more than one character, we need to pass two values. Here\u2019s an example of a <code>slice()<\/code> function that gets the characters between the index values <code>1<\/code> and <code>3<\/code> from our string:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"Career\".slice(1..3)<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>are<\/pre><\/div>\n\n\n\n<p>If we wanted, we could also access our string using its negative index value. This allows us to count backwards from the end of our string, which is useful if we have a long string. The negative index values for the string <code>Career<\/code> would be as follows:<\/p>\n\n\n\n<figure class=\"wp-block-table course-info-table\"><table><tbody><tr><td>C<\/td><td>a<\/td><td>r<\/td><td>e<\/td><td>e<\/td><td>r<\/td><\/tr><tr><td>-6<\/td><td>-5<\/td><td>-4<\/td><td>-3<\/td><td>-2<\/td><td>-1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>So, if we wanted to get the last character in our string, we could use the following code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"Career\".slice(-1)<\/pre><\/div>\n\n\n\n<p>Our code would return: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>r<\/pre><\/div>\n\n\n\n<p>In addition, if we want to turn our string into an array of characters, we can use the <code>chars<\/code> method. Here\u2019s an example of the chars method in action:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"Career\".chars<\/pre><\/div>\n\n\n\n<p>Our code returns the following: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>[\"C\", \"a\", \"r\", \"e\", \"e\", \"r\"]<\/pre><\/div>\n\n\n\n<p>Now that we know the basics of how to retrieve characters from a string and how strings are indexed, we can move on to explore more string operations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>String Length<\/strong><\/h2>\n\n\n\n<p>The <code>length<\/code> method allows us to get the number of characters in a string. This method can be useful if you have a minimum or maximum character limit that you need to impose on a string. For example, you may limit the size of email addresses to 100 characters to prevent people from abusing a form.<\/p>\n\n\n\n<p>Here\u2019s an example of the length method in action:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>name = \"John Appleseed\"\nprint name.length<\/pre><\/div>\n\n\n\n<p>Our code returns <code>14<\/code>. Remember that every character, including spaces, will be included in our total.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Changing Cases<\/strong><\/h2>\n\n\n\n<p>You may want to change the cases in a string with which you are working. For example, you may want all usernames to be in lowercase so that people cannot create two accounts with the same characters capitalized differently.<\/p>\n\n\n\n<p>The functions <code>upcase<\/code> and <code>downcase<\/code> can be used to change the cases of a string to uppercase and lowercase, respectively. Here\u2019s an example of the upcase function being used:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>name = \"John Appleseed\"\nprint name.upcase<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>JOHN APPLESEED<\/pre><\/div>\n\n\n\n<p>We could also replace <code>upcase<\/code> with <code>downcase<\/code> in the above example to make our string appear in all lowercase letters.<\/p>\n\n\n\n<p>You can also use the <code>swapcase<\/code> method if you want to invert the cases in a string. Here\u2019s an example of the swapcase method in action:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>name = \"John Appleseed\"\nprint name.swapcase<\/pre><\/div>\n\n\n\n<p>Our code returns the following: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>jOHN aPPLESEED<\/pre><\/div>\n\n\n\n<p>There is also a function\u2014<code>capitalize<\/code>\u2014that allows us to change the first letter of a string to a capital letter. This may be useful if we want every name in our program to appear with the first letter capitalized, even if a user enters a lowercase character at the start of their name.&nbsp;<\/p>\n\n\n\n<p>Here\u2019s an example of the capitalize function in action:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"john\".capitalize<\/pre><\/div>\n\n\n\n<p>Our code returns the following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>John<\/pre><\/div>\n\n\n\n<p>However, the capitalize function only capitalizes the first character in our string, so if we were to include a surname, it would not be capitalized.<\/p>\n\n\n\n<p>These functions do not modify our strings; instead, they create new ones. If you want to modify the original string, you can use the <code>!<\/code> operator. Here\u2019s an example of a program that will convert all letters in a name to uppercase:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>name = \"Erica\"\nname = name.upcase!\nprint name<\/pre><\/div>\n\n\n\n<p>Our code returns the following:  <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>ERICA<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Finding Text<\/strong><\/h2>\n\n\n\n<p>There may be times when you need to check whether a string contains a substring. We can use the <code>include?()<\/code> method to do this. The method returns <code>true<\/code> if the substring can be found within the string, and <code>false<\/code> if it cannot.<\/p>\n\n\n\n<p>Here\u2019s an example of the <code>include?()<\/code> method in action:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"John Appleseed\".include?(\"Apple\")<\/pre><\/div>\n\n\n\n<p>Our code returns <code>true<\/code>, because our string contains <code>Apple<\/code>.<\/p>\n\n\n\n<p>On the other hand, if we want to get the index value of the character at which a certain substring starts, we can use the <code>index()<\/code> function, like so:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"John Appleseed\".index(\"Apple\")<\/pre><\/div>\n\n\n\n<p>Our code returns <code>5<\/code>, which is the index value of the character at which our substring begins. That said, the index function only returns the first occurance of a value in a string, so if we were looking for multiple occurances of a value, we would not be able to use the index function.<\/p>\n\n\n\n<p>In addition, if we are looking to find out whether our text starts with a particular string, we can use the <code>start_with()<\/code> method. The <code>start_with()<\/code> method accepts one or more strings and will return <code>true<\/code> if any of those strings can be found in the larger string.<\/p>\n\n\n\n<p>Here\u2019s an example of the <code>start_with()<\/code> method:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>name = \"John Appleseed\"\nname.start_with(\"John\", \"Graham\")<\/pre><\/div>\n\n\n\n<p>Our code returns <code>true<\/code>, because our name starts with <code>John<\/code>, which is one of the values we specified.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Replacing Text<\/strong><\/h2>\n\n\n\n<p>If you want to replace text in Ruby, you can do so using the <code>sub()<\/code> and <code>gsub()<\/code> methods. Let\u2019s say that we have a sentence and we want to replace the word <code>quick<\/code> with <code>slow<\/code>. We could use the <code>sub()<\/code> function to do this, like so:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>text = \"The quick brown fox. The quick white bear.\"\nprint text.sub(\"quick\", \"slow\")<\/pre><\/div>\n\n\n\n<p>Our program returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>The slow brown fox. The quick white bear.<\/pre><\/div>\n\n\n\n<p>If we wanted to change every instance of the word <code>quick<\/code> to <code>slow<\/code>, then we would need to use <code>gsub()<\/code>, like so:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>text = \"The quick brown fox. The quick white bear.\"\nprint text.gsub(\"quick\", \"slow\")<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>The slow brown fox. The slow white bear.<\/pre><\/div>\n\n\n\n<p>As you can see, our program has replaced every instance of the word <code>quick<\/code> with <code>slow<\/code>.<\/p>\n\n\n\n<p>It\u2019s worth noting that <code>sub()<\/code> and <code>gsub()<\/code> are case sensitive, so make sure you employ the use of the right cases in your substrings.<\/p>\n\n\n\n<p>Like in other string functions, <code>sub()<\/code> and <code>gsub()<\/code> do not replace a string unless you use the <code>!<\/code> flag. Here\u2019s an example of a text replace function that will edit our original string:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>text = \"The quick brown fox. The quick white bear.\"\ntext = text.gsub!(\"quick\", \"slow\")\nprint text<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>The slow brown fox. The slow white bear.<\/pre><\/div>\n\n\n\n<p>Our original <code>text<\/code> variable has been replaced with the updated one.<\/p>\n\n\n\n<p>For advanced Ruby coders, you may want to consider using <code>regex<\/code> (regular expressions) to conduct a search and replace function. This gives us more control over our replace function. Here\u2019s an example of a gsub that will replace the letters <code>t<\/code> and <code>b<\/code> with <code>X<\/code>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>text = \"The quick brown fox. The quick white bear.\"\ntext = text.gsub \/[tb]\/, \"X\"\nprint text<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>The quick Xrown fox. The quick whiXe Xear.<\/pre><\/div>\n\n\n\n<p>If you\u2019re looking to find out more about Regex and to experiment with different operations, check out <a href=\"https:\/\/regexr.com\/\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\">RegExr<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Padding and Stripping<\/strong><\/h2>\n\n\n\n<p>If you want to format text, you may find yourself in a situation where you want spaces between your strings. Or perhaps you want to remove extra spaces from a string so that you can format your string a certain way.<\/p>\n\n\n\n<p>There are a number of built-in functions in Ruby that make it easy to format strings. Let\u2019s break these down.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Center and Adjust<\/strong><\/h2>\n\n\n\n<p>If you want a string to be centered and surrounded by spaces, you can use the <code>center()<\/code> function. Here\u2019s an example of the <code>center()<\/code> function in action:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"John\".center(20)<\/pre><\/div>\n\n\n\n<p>Our code returns the following: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"        John        \"<\/pre><\/div>\n\n\n\n<p>The <code>center()<\/code> method works with two parameters: (1) the number of spaces you want to add to your string (20 in the below example), and (2) the character you want to use instead of a space if you do not want to use a space. Here\u2019s an example of <code>center()<\/code> being used with both parameters:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"John\".center(20, \"*\")<\/pre><\/div>\n\n\n\n<p>Our code returns the following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>********John********<\/pre><\/div>\n\n\n\n<p>You can also use the <code>ljust()<\/code> and <code>rjust()<\/code> functions if you want to add spaces to the left or right of a string, respectively. Here\u2019s an example of the <code>ljust()<\/code> function in action: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"John\".ljust(20, \"*\")<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>John****************<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Strip and Chomp<\/strong><\/h2>\n\n\n\n<p>The <code>center()<\/code>, <code>ljust()<\/code>, and <code>rjust()<\/code> functions can be used to add spaces or other characters to a string. But what if you want to remove characters from a string? That\u2019s where the <code>strip<\/code>, <code>chop<\/code> and <code>chomp()<\/code> functions come in.<\/p>\n\n\n\n<p>Strip works in the same way as the <code>center()<\/code>, <code>ljust()<\/code>, and <code>rjust()<\/code> functions do. The <code>strip<\/code>, <code>lstrip<\/code>, and <code>rstrip<\/code> functions remove all spaces, spaces on the left, and spaces on the right in a string, respectively. Here\u2019s an example of these in action:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"             John   \".strip # \"John\"\n\"John                \".lstrip # \"John\"\n\"                John\".rstrip # \"John\"<\/pre><\/div>\n\n\n\n<p>In addition, we can use chop to remove the last character in a string. Here\u2019s an example of chop in action: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"Pauline\".chop<\/pre><\/div>\n\n\n\n<p>Our code returns: <code>Paulin<\/code>. <\/p>\n\n\n\n<p>We can also use <code>chomp<\/code> to remove multiple characters from the end of a string. Here\u2019s an example of chomp being used to remove the last two characters from our string: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"Pauline\".chomp(\"ne\")<\/pre><\/div>\n\n\n\n<p>Our code returns: <code>Pauli<\/code>. <\/p>\n\n\n\n<p>If you don\u2019t specify the characters you want to remove with <code>chomp()<\/code>, it will remove a newline character (\u201c<code>\\n<\/code>\u201d). If there are no newline characters in your string, <code>chomp()<\/code> will return the original string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>String methods allow us to manipulate a string according to our needs. In this tutorial, we explored how to manipulate strings using several built-in Ruby functions. We discussed how strings are made in Ruby, how to access strings using their index values, how to replace text, how to find text within a string, how to convert cases in a string, and other functions.<\/p>\n\n\n\n<p>Now you\u2019re an expert at working with Ruby string methods!<\/p>\n","protected":false},"excerpt":{"rendered":"Manipulating text is a common operation in many programming languages, and Ruby is no exception. For example, you may want to capitalize someone\u2019s name before it is printed on an invoice, or you may want to replace someone\u2019s apartment number if they have moved. In this guide, we break down the most useful string methods&hellip;","protected":false},"author":240,"featured_media":12498,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17278],"tags":[],"class_list":{"0":"post-12493","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-ruby"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Ruby","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":"","is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Ruby String Methods: A Complete Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"Ruby contains a number of string methods that can be used to manipulate strings. On Career Karma, learn about how to work with Ruby strings and how to use the most popular Ruby string methods.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ruby String Methods\" \/>\n<meta property=\"og:description\" content=\"Ruby contains a number of string methods that can be used to manipulate strings. On Career Karma, learn about how to work with Ruby strings and how to use the most popular Ruby string methods.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-02-24T21:24:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T10:30:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/RUBY-STRING-METHODS.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"668\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Ruby String Methods\",\"datePublished\":\"2020-02-24T21:24:16+00:00\",\"dateModified\":\"2023-12-01T10:30:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/\"},\"wordCount\":1626,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/RUBY-STRING-METHODS.jpg\",\"articleSection\":[\"Ruby\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/\",\"name\":\"Ruby String Methods: A Complete Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/RUBY-STRING-METHODS.jpg\",\"datePublished\":\"2020-02-24T21:24:16+00:00\",\"dateModified\":\"2023-12-01T10:30:25+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"Ruby contains a number of string methods that can be used to manipulate strings. On Career Karma, learn about how to work with Ruby strings and how to use the most popular Ruby string methods.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/RUBY-STRING-METHODS.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/RUBY-STRING-METHODS.jpg\",\"width\":1000,\"height\":668},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ruby\",\"item\":\"https:\/\/careerkarma.com\/blog\/ruby\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Ruby String Methods\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Ruby String Methods: A Complete Guide | Career Karma","description":"Ruby contains a number of string methods that can be used to manipulate strings. On Career Karma, learn about how to work with Ruby strings and how to use the most popular Ruby string methods.","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:\/\/careerkarma.com\/blog\/ruby-string-methods\/","og_locale":"en_US","og_type":"article","og_title":"Ruby String Methods","og_description":"Ruby contains a number of string methods that can be used to manipulate strings. On Career Karma, learn about how to work with Ruby strings and how to use the most popular Ruby string methods.","og_url":"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-02-24T21:24:16+00:00","article_modified_time":"2023-12-01T10:30:25+00:00","og_image":[{"width":1000,"height":668,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/RUBY-STRING-METHODS.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Ruby String Methods","datePublished":"2020-02-24T21:24:16+00:00","dateModified":"2023-12-01T10:30:25+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/"},"wordCount":1626,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/RUBY-STRING-METHODS.jpg","articleSection":["Ruby"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/ruby-string-methods\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/","url":"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/","name":"Ruby String Methods: A Complete Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/RUBY-STRING-METHODS.jpg","datePublished":"2020-02-24T21:24:16+00:00","dateModified":"2023-12-01T10:30:25+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"Ruby contains a number of string methods that can be used to manipulate strings. On Career Karma, learn about how to work with Ruby strings and how to use the most popular Ruby string methods.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/ruby-string-methods\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/RUBY-STRING-METHODS.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/RUBY-STRING-METHODS.jpg","width":1000,"height":668},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/ruby-string-methods\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Ruby","item":"https:\/\/careerkarma.com\/blog\/ruby\/"},{"@type":"ListItem","position":3,"name":"Ruby String Methods"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/12493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=12493"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/12493\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/12498"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=12493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=12493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=12493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}