[Ruby] Cut out a string using the slice method

__ I want to cut out only a specific part from a character string. At that time __

slice method

By using the slice method, the specified element can be extracted from the array or character string.

#Create an array
array = [0,1,2,3,4,5,6]

#Slices the element specified by the argument from the array
ele1 = array.slice(1)
puts ele1
#=> 1

#Slice the elements for array numbers 1 to 4
ele2 = array.slice(1,4)
puts ele2
#=> 1 2 3 4

#The array remains the same
puts array 
#=> [0,1,2,3,4,5,6]  

Problem example

Create a method that outputs the last two characters from an arbitrary character string.

extra_end('Hello') → 'lo'
extra_end('abcde') → 'de'

Here, the method to be called is extra_end.
Now let's write the code.

def extra_end(str)
  char_num = str.length
  right2 = str.slice(char_num - 2, 2)
  puts right2
end

First, let the formal argument of extra_end be str and prepare to receive the actual argument. char_num is defined using the length method to get the number of characters in str.
The problem this time is "get the last 2 characters", so use the slice method to set it to -2 of char_num, that is, go back two characters counting from the back, and then set it to 2, and then two characters from there. Get the minutes.
For example, if you call the method with extra_end ('Hello'), char_num = 5 and right2 = str.slice (3,2). slice (3,2) cuts two elements counting from the third array number (index). In this case, the cut result lo remains, and right2 = lo.

■ Reference reference

Recommended Posts

[Ruby] Cut out a string using the slice method
Cut out a Ruby string
[Ruby] Get in the habit of using the dup method when making a copy of a string variable
[Ruby] Obtaining even values ​​using the even? Method
[Ruby] slice method
Come out with a suffix on the method
Come out with a suffix on the method 2
[Ruby basics] How to use the slice method
Count the number of occurrences of a string in Ruby
Examine the elements in the array using the [Ruby] includes? Method
[Java] How to cut out a character string character by character
[Java] Cut out a part of the character string with Matcher and regular expression
Creating a calendar using Ruby
Create a fortune using Ruby
[Ruby] Count an even number in an array using the even? Method
The nth and n + 1st characters of a Ruby string
[Java] How to use substring to cut out a character string
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
Java comparison using the compareTo () method
[Ruby] Extracting elements with slice method
String output method memo in Ruby
[Rails 6] destroy using the resources method
[Ruby] Search problem using index method
How to operate IGV using socket communication, and the story of making a Ruby Gem using that method
Invoke the character string passed as an argument as a method with send
[Java] How to use substring to cut out a part of a character string
[Java] How to get to the front of a specific string using the String class
Iterative processing of Ruby using each method (find the sum from 1 to 10)
[Ruby] From the basics to the inject method
[Ruby] Cut off the contents of twitter-ads
Extract a part of a string with Ruby
Get the error message using the any? method
Extract characters from Ruby strings slice method
[Ruby] I want to make an array from a character string with the split method. And vice versa.
If it is Ruby, it is efficient to make it a method and stock the processing.
Call a method of the parent class by explicitly specifying the name in Ruby
[Ruby] How to calculate the total amount using the initialize method and class variables
[Ruby] What is the slice method? Let's solve the example and understand the difference from slice!
[Ruby] Sending a POST HTTP request to the Web API using OAuth authentication
A memorandum to clean up the code Ruby
[Delete the first letter of the character string] Ruby
[Ruby] I want to do a method jump!
Output of how to use the slice method
[Ruby] Exclude duplicate elements with the uniq method.
[Ruby] How to generate a random alphabet string
Finding pi with the Monte Carlo method? (Ruby)
[Ruby] Get a 3-digit integer and conditionally branch using an if statement (digits method)