Output of how to use the slice method

As the title suggests, it outputs about the slice method.

What is the slice method?

The specified element can be extracted from the array or character string.

Various ways to use

Part 1 (Specify one and take it out)

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

result = array.slice(1)
puts result
#=> 1

#The array itself does not change
puts array 
#=> [0,1,2,3,4,5,6]

Part 2-1 (take out multiple elements)

You can also retrieve multiple elements.

The following is the method of "taking out ○ pieces from here".

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

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

You can also use this method to:

#Create a method that outputs the last two characters of an arbitrary character string three times repeatedly

def extra_end(str)
  num = str.length
  right2 = str.slice(num - 2, 2)
  puts right2 * 3
end

extra_end(str)

Get the number of characters in the string with str.length. Since the character string is the same as the array and the beginning is counted from 0, To get the last character of a string, use str.slice (num -1). This time I want to get the last two characters of the string, so set it to str.slice (num --2, 2).

Part 2-2

Another way to retrieve multiple elements There is also a method of "specifying and extracting sequence numbers ○ to X".

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

#Slice the elements of sequence numbers 1 to 4
result = array.slice(1..4)
puts result
#=> 1 2 3 4

You can also do this.

#Define elements
array = "string"

#Of the sequence number-From 3-Cut out a string in the range of 1
result = array.slice(-3..-1)
puts result
#=> "ing"

The rightmost is -1, and the sequence numbers are counted from right to left as -1, -2, -3 ....

Part 3 slice!

You can change the original array or string by adding! After the slice. (Destructive method)

string = "abcde"

result = string.slice!(2)
puts result
#=> "c"

# "c"Has been removed
puts string
#=> "abde"

Recommended Posts

Output of how to use the slice method
[Ruby basics] How to use the slice method
How to use the link_to method
How to use the include? method
How to use the form_with method
[Rails] How to use the map method
[Java] How to use the toString () method
How to use the replace () method (Java Silver)
[Java] How to use compareTo method of Date class
[Java] How to use join method
How to use the wrapper class
How to use setDefaultCloseOperation () of JFrame
[Ruby] How to use any? Method
How to use Ruby inject method
How to use the getter / setter method (in object orientation)
[Java] How to use the File class
[Java] How to use the hasNext function
How to use submit method (Java Silver)
[Java] How to use the HashMap class
Studying how to use the constructor (java)
How to determine the number of parallels
[Ruby] How to use slice for beginners
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to sort the List of SelectItem
Output of the book "Introduction to Java"
[Processing × Java] How to use the function
[Java] How to use the Calendar class
[Rails] How to use helper method, confimartion
[Rails] How to omit the display of the character string of the link_to method
Output about the method # 2
How to find the cause of the Ruby error
How to output the sum of any three numbers, excluding the same value
I want to output the day of the week
[Java] How to use Thread.sleep to pause the program
Customize how to divide the contents of Recyclerview
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use java.util.logging
How to use map
[Ruby] How to use gsub method and sub method
How to get today's day of the week
How to use Twitter4J
When you want to use the method outside
How to use active_hash! !!
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use enum (introduction of Japanese notation)
How to use JUnit 5
How to use JQuery in js.erb of Rails6
How to display the result of form input
How to use Dozer.mapper
How to use Gradle
[Ruby on Rails] How to use session method
[Java] How to get the authority of the folder
How to use org.immutables
How to use java.util.stream.Collector