{"id":32091,"date":"2018-11-04T19:22:56","date_gmt":"2018-11-04T13:52:56","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=32091"},"modified":"2018-11-04T19:24:47","modified_gmt":"2018-11-04T13:54:47","slug":"work-with-string-methods-in-ruby","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/work-with-string-methods-in-ruby\/","title":{"rendered":"To Work with String Methods in Ruby"},"content":{"rendered":"<p><span style=\"color: #003366;\"><strong>Work with String Methods in Ruby<\/strong><\/span><\/p>\n<h3 id=\"introduction\"><span style=\"color: #008080;\">Introduction<\/span><\/h3>\n<p><a href=\"https:\/\/www.wikitechy.com\/tutorials\/ruby-on-rails\/\" target=\"_blank\" rel=\"noopener\">Ruby<\/a> strings have several inbuilt ways that create it straightforward to switch and manipulate text, a standard task in several programs.<\/p>\n<p>You&#8217;ll use string ways to see the <strong>length of a string<\/strong>, <strong>index and split strings to extract substrings,<\/strong> add and take away whitespace and alternative characters, amendment the case of characters in strings, and realize and replace text.<\/p>\n<p>When you are done, you will be ready to incorporate these ways into your own programs.<\/p>\n<h3 id=\"determining-string-length\"><span style=\"color: #0000ff;\">Determining String Length<\/span><\/h3>\n<p><strong>String methodology<\/strong> length returns the number of characters during a <a href=\"https:\/\/www.wikitechy.com\/forum\/d\/703-solved-string-ruby-capitalize-every-word-first-letter\" target=\"_blank\" rel=\"noopener\">string<\/a>. This methodology is helpful for once you ought to enforce the minimum or most word <strong>lengths, or to truncate larger string<\/strong>s to be at intervals sure limits to be used as abbreviations.<\/p>\n<p>The <strong>example<\/strong> that prints the length of a sentence:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;open_source%20%3D%20%22Sammy%20contributes%20to%20open%20source.%22%0Aprint%20open_source.length&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h3 id=\"output\"><span style=\"color: #008000;\">Output:<\/span><\/h3>\n<pre>33<\/pre>\n<p>Keep in mind that each character, together with<strong> letters, numbers, whitespace characters, and symbols<\/strong>, are counted since it&#8217;s a part of the string.<\/p>\n<p>To check to examine if a<strong> string is empty<\/strong>, you&#8217;ll check to examine if its length is zero, otherwise, you will use the <strong>empty?<\/strong> method:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;name%20%3D%20%22%22%0Aname.empty%3F%20%20%20%20%23%20true%0A%0Aname%20%3D%20%22Sammy%22%0Aname.empty%3F%20%20%20%20%23%20false%0A%0Aname%20%3D%20%22%20%20%20%20%20%22%0Aname.empty%3F%20%20%20%20%23%20false&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>Let&#8217;s check up on the way to we tend to index strings and access a string&#8217;s contents.<\/p>\n<h3 id=\"accessing-characters-at-intervals-a-string\"><span style=\"color: #0000ff;\">Accessing Characters at intervals a String<\/span><\/h3>\n<p>To print or work with a <strong>number of the characters<\/strong> during a string, use the slice methodology to urge the half you would like.<\/p>\n<p>Like <a href=\"https:\/\/www.wikitechy.com\/tutorials\/javascript\/sort-array-of-objects-by-string-property-value-in-javascript\" target=\"_blank\" rel=\"noopener\">arrays<\/a>, wherever every component corresponds to an index, every of a string\u2019s characters additionally corresponds to an index, beginning with the index zero.<\/p>\n<p>For the <strong>string Sammy<\/strong> the index breakdown sounds like this:<\/p>\n<table border=\"1\" width=\"250\" align=\"center\">\n<thead>\n<tr>\n<td style=\"text-align: center;\"><strong>o<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>2<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>3<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>4<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">S<\/td>\n<td style=\"text-align: center;\">a<\/td>\n<td style=\"text-align: center;\">m<\/td>\n<td style=\"text-align: center;\">m<\/td>\n<td style=\"text-align: center;\">y<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Slice methodology allows you to grab one character or a variety of characters. Passing one whole number returns the character at that index. <strong>Passing 2 integers<\/strong>, separated by a comma, tells slice to come back all the characters from the primary index to the last index, inclusive. The slice methodology additionally accepts a variety, such as 1..4, to specify the characters to extract:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22Sammy%22.slice(0)%20%20%20%20%20%23%20%22s%22%0A%22Sammy%22.slice(1%2C2)%20%20%20%23%20%22am%22%0A%22Sammy%22.slice(1..4)%20%20%23%20%22ammy%22&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>The [ ] syntax is AN alias for the slice, therefore you&#8217;ll treat strings like arrays:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22Sammy%22%5B0%5D%20%20%20%20%20%23%20%22s%22%0A%22Sammy%22%5B1%2C2%5D%20%20%20%23%20%22am%22%0A%22Sammy%22%5B1..4%5D%20%20%23%20%22ammy%22&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>You may also access one character from the tip of the string with a negative index. <strong>-1<\/strong> would allow you to access the last character of the string, <strong>-2<\/strong> would access the second-to-last, and so on.<\/p>\n<p>Finally, you&#8217;ll convert the string to an array of characters with the chars method:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22sammy%22.chars%20%20%20%20%23%20%5B%22S%22%2C%20%22a%22%2C%20%22m%22%2C%20%22m%22%2C%20%22y%22%5D&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>This will be helpful for <strong>manipulating or remodeling<\/strong> the characters within the string.<\/p>\n<p>Next, let&#8217;s check up on the way to alter the case of the characters during a string.<\/p>\n<h3 id=\"converting-to-higher-and-lower-case-letter\"><span style=\"color: #0000ff;\">Converting to higher and lower-case letter<\/span><\/h3>\n<p>The <strong>upcase and downcase<\/strong> ways come back a string with all the letters of a creative string regenerate to upper- or lower-case letters. Any <a href=\"https:\/\/www.wikitechy.com\/step-by-step-tutorials\/php\/php-character-sort\" target=\"_blank\" rel=\"noopener\">characters<\/a> within the string that aren&#8217;t letters won&#8217;t be modified.<\/p>\n<p>Let\u2019s convert the <strong>string Sammy Shark<\/strong> to be all higher case:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;name%20%3D%20%22Sammy%20Shark%22%0Aprint%20name.upcase&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h3 id=\"output-2\"><span style=\"color: #008000;\">Output:<\/span><\/h3>\n<pre>SAMMY SHARK<\/pre>\n<p>Now, let\u2019s convert the <strong>string to be all lower case:<\/strong><\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;print%20name.downcase&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h3 id=\"output-3\"><span style=\"color: #008000;\">Output:<\/span><\/h3>\n<pre>SAMMY SHARK<\/pre>\n<p><strong>Upcase and downcase functions<\/strong> create it easier to judge and compare strings by creating case consistent throughout. for instance, if you fire a username and therefore the user enters the username with a capital, you&#8217;ll minuscule the user&#8217;s input and compare it against a minuscule best-known price.<\/p>\n<p><strong>Ruby strings<\/strong> even have a capitalize methodology that returns a replacement string with the primary character capitalized:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22sammy%22.capitalize%20%20%20%20%23%20%22Sammy%22&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>This may be a <strong>convenient methodology<\/strong>, however, use caution however you employ it; it solely capitalizes the primary letter, therefore, it&#8217;d not perpetually match the employment case you would like.<\/p>\n<p>It additionally provides a <strong>swapcase methodology<\/strong> that returns a string with the casing swapped:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;text%20%3D%20%22Sammy%22%0Aprint%20text.swapcase&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h3 id=\"output-4\"><span style=\"color: #008000;\">Output<\/span><\/h3>\n<pre class=\"code-pre \"><code>sAMMY<\/code><\/pre>\n<p>The <strong>downcase, upcase, capitalize<\/strong> and <strong>swapcase<\/strong> ways all come back a replacement string and leave the present string unedited. this is often necessary to recollect if you are doing one thing aside from straight off printing out the text. Take a glance at the subsequent example:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;text%20%3D%20%22sammy%22%0Atext.capitalize%0A%0Aprint%20%22Hello%2C%20%23%7Btext%7D!%22&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h3 id=\"output-5\"><span style=\"color: #008000;\">Output:<\/span><\/h3>\n<pre class=\"code-pre \"><code>Hello, sammy!<\/code><\/pre>\n<p>Even tho&#8217; we tend to know as exploit the text variable, we tend to ne&#8217;er captured the worth came by capitalizing. We&#8217;d rewrite the program like this:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;text%20%3D%20%22sammy%22%0Atext%20%3D%20text.capitalize%0A%0Aprint%20%22Hello%2C%20%23%7Btext%7D!%22&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h3 id=\"output-6\"><span style=\"color: #008000;\">Output:<\/span><\/h3>\n<pre class=\"code-pre \"><code>Hello, Sammy!<\/code><\/pre>\n<p>You will use <strong>downcase!, upcase!, capitalize! and swapcase!<\/strong> to switch the first string instead:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;text%20%3D%20%22sammy%22%0Atext%20%3D%20text.capitalize!%0A%0Aprint%20%22Hello%2C%20%23%7Btext%7D!%22&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p><strong>Be careful<\/strong> tho&#8217;. There area unit disadvantages to mutating the first string. Ruby provides each way, therefore, you&#8217;ll opt for the one that matches your wants.<\/p>\n<p>Now let&#8217;s add and take away whitespace from strings.<\/p>\n<h3 id=\"padding-and-baring-strings\"><span style=\"color: #0000ff;\">Padding and baring Strings<\/span><\/h3>\n<p>If you are writing a program that has got to format some text, you may typically realize that you will need to feature some area before of, after, or around a string so as to form it line up with alternative information.<\/p>\n<p>And alternative times, you will need to <strong>get rid of gratuitous characters<\/strong> from the start or finish of your strings, like additional whitespace or special characters.<\/p>\n<p>To <strong>surround a string with areas<\/strong>, use the middle method:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22Sammy%22%2Ccenter(21)%20%20%20%20%23%20%22%20%20%20%20%20%20%20%20Sammy%20%20%20%20%20%20%20%20%22&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>You will specify a string because of the <strong>second argument<\/strong> if you wish to use a distinct character:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22%20%5BSammy%5D%20%22.center(21%2C%20%22%3C%3E%22)%20%20%20%23%20%22%3C%3E%3C%3E%3C%3E%20%5BSammy%5D%20%3C%3E%3C%3E%3C%3E%22&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>The <strong>ljust and rjust<\/strong> ways add areas or characters to the <strong>left or right facet<\/strong> of a string and work specifically just like the center method:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22Sammy%22.ljust(20)%20%20%20%20%20%20%20%20%20%23%20%22Sammy%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%0A%22Sammy%22.rjust(20)%20%20%20%20%20%20%20%20%20%23%20%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Sammy%22%0A%22Sammy%22.rjust(20%2C%20%22!%22)%20%20%20%20%23%20%22!!!!!!!!!!!!!!!Sammy%22&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>To take away leading areas from a string, use the<strong> rstrip methodology<\/strong>. To get rid of trailing areas, use lstrip. Use strip to get rid of each leading and trailing spaces:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22%20%20%20%20%20%20%20%20Sammy%22.rstrip%20%20%20%20%23%20%22Sammy%22%0A%22Sammy%20%20%20%20%20%20%20%20%22.lstrip%20%20%20%20%23%20%22Sammy%22%0A%22%20%20%20%20%20%20Sammy%20%20%22.strip%20%20%20%20%20%23%20%22Sammy%22&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>You will use the <strong>center!, ljust!, rjust!, lstrip!, rstrip!, and strip!<\/strong> ways to switch the first string.<\/p>\n<p>Sometimes you&#8217;ll have to <strong>get rid of characters from the tip of a string<\/strong>. <strong>Ruby&#8217;s chop methodology<\/strong> will simply that; it removes the last character from a string:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22Sammy%22.chop%20%20%20%20%20%23%20%22Samm%22&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>This is particularly helpful for<strong> removing the newline character<\/strong> (\\n) from strings:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22This%20string%20has%20a%20newline%5Cn%22.chop&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>The <strong>chop methodology<\/strong> leaves the first string intact, returning a replacement string. The chop! methodology modifies the present string in situ.<\/p>\n<p>The chomp methodology will take away multiple characters from the tip of a string:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22Sammy%22.chomp(%22my%22)%20%20%20%20%23%20%22Sam%22&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>If you do not specify a <strong>string to get rid of, chomp<\/strong> can take away the newline:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22This%20string%20has%20a%20newline%5Cn%22.chomp%20%20%20%23%20%22This%20string%20has%20a%20newline&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>However, if the string does not contain a <strong>newline character<\/strong>, chomp simply returns the first string:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22Sammy%22.chomp%20%20%20%20%20%23%20%22Sammy%22&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>This makes chomp a touch safer to use once removing newlines than the chop methodology, that perpetually removes the last character.<\/p>\n<p><strong>Ruby contains a chomp!<\/strong> a methodology that mutates the first string and returns the modified string if it performed a replacement. However, not like chomp, the chomp! methodology returns null if it did not alter the string:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;string%20%3D%20%22Hello%5Cn%22%0Astring.chomp!%20%20%20%20%20%23%20%22Hello%22%0A%0Astring%20%3D%20%22Hello%22%0Astring.chomp!%20%20%20%20%20%23%20nil&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>Next, let&#8217;s check up on the way to seek for text in strings.<\/p>\n<h3 id=\"finding-characters-and-text\"><span style=\"color: #0000ff;\">Finding Characters and Text<\/span><\/h3>\n<p>Sometimes you would like to see whether or not or not a string contains another string.<\/p>\n<p>The <strong>include?<\/strong> methodology checks to examine if a string contains another string. It returns true if the string exists and false if not:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22Sammy%22.include%3F(%22a%22)%20%20%20%23%20true%0A%22Sammy%22.include%3F(%22b%22)%20%20%20%23%20false&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>The <strong>index methodology<\/strong> returns the index of a personality. It may also establish the index of the primary character of a substring. And it returns null if the character or substring does not exist:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22Sammy%22.index(%22a%22)%20%20%20%20%20%23%201%0A%22Sammy%22.index(%22mm%22)%20%20%20%20%23%202%0A%22Sammy%22.index(%22Fish%22)%20%20%23%20nil&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>The <strong>index methodology<\/strong> solely finds the primary occurrence tho&#8217;. Here&#8217;s AN example with an extended string:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;text%20%3D%20%22Sammy%20contains%20a%20balloon%22%0Atext.index(%22a%22)%20%20%20%20%23%201&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>The<strong> string Sammy<\/strong> contains a balloon has four occurrences of the letter &#8220;a&#8221;. However, index solely found the primary occurrance. you will have to jot down one thing a lot of specific to find one amongst the opposite occurrances.<\/p>\n<p>For example, you may convert the string to AN array of characters and use array ways to tell through the results and choose the indices for the character. Here&#8217;s one methodology for doing that:<\/p>\n<pre class=\"code-pre \"><code>[1, 7, 10, 13]<\/code><\/pre>\n<p><strong>each_with_index<\/strong> returns a two-dimensional array containing the an entry for every character and its index. choose whittles it all the way down to simply the entries wherever the character may be a, and map converts the 2-dimensional array into a one-dimensional array of the indices.<\/p>\n<p>In addition to trying to find characters during a string, you&#8217;ll check to examine if a string starts with a personality or substring victimization the start_with? method:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;text%20%3D%20%22Sammy%20contains%20a%20balloon%22%0Atext.start_with%3F(%22s%22)%20%20%20%20%20%20%20%20%20%23%20true%0Atext.start_with%3F(%22Sammy%20has%22%20%20%23%20true&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>The <strong>start_with?<\/strong> methodology accepts multiple strings and returns true if any of them match:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;text%20%3D%20%22Sammy%20contains%20a%20balloon%22%0Atext.start_with%3F(%22Sammy%20the%20Shark%22%2C%20%22Sammy%22)%20%23%20true&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>In this instance, &#8220;<strong>Sammy the Shark<\/strong>&#8221; is not found, however, &#8220;<strong>Sammy&#8221;<\/strong> is, therefore the comeback price is true.<\/p>\n<p>You will use the <strong>end_with?<\/strong> methodology to examine if a string ends with the given substring. It works specifically like<strong> start_with?:<\/strong><\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;text%20%3D%20%22Sammy%20contains%20a%20balloon%22%0Atext.end_with%3F(%22balloon%22)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20true%0Atext.end_with%3F(%22boomerang%22)%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20false%0Atext.end_with%3F(%22boomerang%22%2C%20%22balloon%22)%20%20%23%20tru&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>We&#8217;ve checked out ways that to search out text, therefore let&#8217;s check up on the way to replace that text with completely different text.<\/p>\n<h3 id=\"replacing-text-in-strings\"><span style=\"color: #0000ff;\">Replacing Text in Strings<\/span><\/h3>\n<p>The realize and <em>replace feature<\/em> in word processors allows you to seek for a string and replace it with another string. you&#8217;ll try this in Ruby with the sub and gsub ways.<\/p>\n<p>The <strong>sub methodology<\/strong> replaces a part of a string with another.<\/p>\n<p>Sammy now not has the balloon; it flew away. Let&#8217;s amendment the substring &#8220;<strong>has&#8221; to &#8220;had&#8221;.<\/strong><\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;balloon%20%3D%20%22Sammy%20contains%20a%20balloon%22%0Aprint%20balloon.sub(%22has%22%2C%22had%22)&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h3 id=\"output-7\"><span style=\"color: #008000;\">Output<\/span><\/h3>\n<pre class=\"code-pre \"><code>Sammy had a balloon.<\/code><\/pre>\n<p>The <strong>sub methodology<\/strong> solely replaces the primary occurrence of the match with the new text. Let&#8217;s use a changed string that has 2 occurrences of the word has:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;balloon%20%3D%20%22Sammy%20has%20a%20balloon.%20The%20balloon%20has%20a%20ribbon%22%0Aprint%20balloon.sub(%22has%22%2C%22had%22)&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h3 id=\"output-8\"><span style=\"color: #008000;\">Output<\/span><\/h3>\n<pre class=\"code-pre \"><code>Sammy <span class=\"highlight\">had<\/span> a balloon. The balloon has a ribbon<\/code><\/pre>\n<p>Only the <strong>primary occurrence<\/strong> modified.<\/p>\n<p>To amendment all of them, use the gsub methodology, that performs international substitution:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;balloon%20%3D%20%22Sammy%20has%20a%20balloon.%20The%20balloon%20has%20a%20ribbon%22%0Aprint%20balloon.gsub(%22has%22%2C%22had%22)&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h3 id=\"output-9\"><span style=\"color: #008000;\">Output:<\/span><\/h3>\n<p class=\"code-pre \"><code>Sammy <span class=\"highlight\">had<\/span> a balloon. The balloon <span class=\"highlight\">had<\/span> a ribbon<\/code><\/p>\n<p>The<strong> sub and gsub<\/strong> ways perpetually come back new strings, departure the originals unqualified. Let&#8217;s demonstrate this by ever-changing &#8220;balloon&#8221; to &#8220;boomerang&#8221; in our string:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;text%20%3D%20%22Sammy%20has%20a%20balloon%22%0Atext.gsub(%22ballooon%22%2C%20%22boomerang%22)%0Aprint%20text&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h3 id=\"output-10\"><span style=\"color: #008000;\">Output:<\/span><\/h3>\n<p class=\"code-pre \"><code>Sammy has a balloon<\/code><\/p>\n<p>The output does not show the result we&#8217;re trying to find, as a result of whereas we tend to did specify the substitution, we tend to never assign gsub to a replacement variable. to urge the result we&#8217;d like, we tend to may rewrite the program like this:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;text%20%3D%20%22Sammy%20has%20a%20balloon%22%0Atext%20%3D%20text.sub(%22ballooon%22%2C%20%22boomerang%22)%0Aprint%20text&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>Alternatively, you&#8217;ll <strong>use sub!<\/strong> instead, that modifies the first string. Let&#8217;s do this by doing a handful of string replacements. We&#8217;ll amendment <strong>&#8220;red balloon&#8221; to &#8220;blue boomerang&#8221;:<\/strong><\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;text%20%3D%20%22Sammy%20has%20a%20red%20balloon%22%0Atext.sub!(%22red%22%2C%20%22blue%22)%0Atext.sub!(%22balloon%22%2C%20%22boomerang%22)%0Aprint%20text&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h3 id=\"output-11\"><span style=\"color: #008000;\">Output:<\/span><\/h3>\n<pre class=\"code-pre \"><code>Sammy has a blue boomerang<\/code><\/pre>\n<p>You will use the <strong>gsub! methodology<\/strong> to try to to a worldwide substitution in situ further.<\/p>\n<p>The sub and gsub ways settle for regular expressions for the search pattern. Let&#8217;s replace all the vowels within the string with the <strong>@ symbol:<\/strong><\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22Sammy%20has%20a%20red%20balloon%22.gsub%20%2F%5Baeiou%5D%2F%2C%20%22%40%22&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22S%40mmy%20h%40s%20%40%20r%40d%20b%40ll%40%40n%22&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>The replacement price does not need to be a string. You&#8217;ll use a hash to specify however individual characters or items ought to get replaced. Let&#8217;s replace all occurrences of the letter a with<strong> @ a<\/strong>nd every one the o characters with zeros:<\/p>\n[pastacode lang=&#8221;ruby&#8221; manual=&#8221;%22Sammy%20has%20a%20red%20balloon%22.gsub%20%2F%5Baeiou%5D%2F%2C%20%7B%22a%22%20%3D%3E%20%22%40%22%2C%20%22o%22%20%3D%3E%20%220%22%7D%0A%23%20%22S%40mmy%20h%40s%20%40%20rd%20b%40ll00n%22&#8243; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>You will use this to perform a lot of advanced substitutions with less code.<\/p>\n<h3 id=\"conclusion\"><span style=\"color: #003366;\">Conclusion<\/span><\/h3>\n<p>You worked with and <strong>manipulated strings<\/strong> victimization a number of the inbuilt ways for the string information kind. you furthermore might learn that a lot of the ways for operating with strings are available 2 variants: one that leaves the string unchanged, and one that <strong>modifies the first string.<\/strong> that one you employ depends on your wants. Ruby provides you the flexibleness to decide on however you wish to figure together with your information. However, writing code that does not modify existing information will be easier to right later.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Work with String Methods in Ruby Introduction Ruby strings have several inbuilt ways that create it straightforward to switch and manipulate text, a standard task in several programs. You&#8217;ll use string ways to see the length of a string, index and split strings to extract substrings, add and take away whitespace and alternative characters, amendment [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86323,83549],"tags":[86324,86325,86326,86327,86328,86329,86330,86331,86332,86334,86333,86335,86337,86336,86338,86339],"class_list":["post-32091","post","type-post","status-publish","format-standard","hentry","category-ruby","category-strings","tag-ruby-index-of-string","tag-ruby-remove-characters-from-beginning-of-string","tag-ruby-remove-characters-from-end-of-string","tag-ruby-remove-characters-from-string","tag-ruby-remove-characters-from-string-regex","tag-ruby-remove-substring","tag-ruby-replace-character-in-string","tag-ruby-replace-multiple-characters-in-string","tag-ruby-replace-substring","tag-ruby-split","tag-ruby-string-concatenation","tag-ruby-string-contains","tag-ruby-string-gsub","tag-ruby-string-replace","tag-ruby-substring-from-character","tag-ruby-substring-to-end"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/32091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=32091"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/32091\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=32091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=32091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=32091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}