[Ruby basics] How to use the slice method

The slice method is a method that can "slice a character string".

For example, _slice (0,5) _ cuts out the character string from the 0th () to the 5th before (that is, the 4th). () Note that, like the array, it does not start from the 1st but starts from the 0th.

def slice_1st(lyrics)
  lyrics.slice(0,5) #Cut out from the 0th to the 5th before (that is, the 4th) of the given character string.
end
p slice_1st("Toy chachacha")*2 #*Output for 2 times with 2.
#=>"Toy toys"

When _slice (0..5) _ is described, the 0th to 5th character strings are cut out.


def slice_2nd(lyrics)
  lyrics.slice(0..5) #Cut out from 0th to 5th of the given character string.
end
p slice_2nd("Cha-cha-cha toys")
#=>"Cha-cha-cha"
p slice_2nd("Cha-cha-cha")
#=>"Cha-cha-cha"

Of course, it is also possible to cut out the middle of the character string.


def slice_3rd(lyrics)
  lyrics.slice(6..12) #Cut out the 6th to 12th of the given character string.
end
p slice_3rd("Don't Stop Me Now")
#=>"Stop Me"

Recommended Posts

[Ruby basics] How to use the slice method
Output of how to use the slice method
How to use the link_to method
How to use the include? method
[Ruby] How to use any? Method
How to use Ruby inject method
[Ruby] From the basics to the inject method
[Rails] How to use the map method
[Java] How to use the toString () method
[Ruby] How to use slice for beginners
[Ruby] How to use gsub method and sub method
How to use the replace () method (Java Silver)
[Ruby on Rails] How to use session method
How to use Ruby return
Ruby: How to use cookies
[Java] How to use join method
How to use the wrapper class
How to use Ruby on Rails
How to use the getter / setter method (in object orientation)
[Ruby] slice method
[Java] How to use the File class
[Ruby on Rails] How to use CarrierWave
[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)
[Processing × Java] How to use the loop
Ruby length, size, count How to use
[Processing × Java] How to use the class
[Ruby on Rails] How to use redirect_to
[Processing × Java] How to use the function
[Ruby on Rails] How to use kaminari
[Java] How to use the Calendar class
Ruby: CSV :: How to use Table Note
[Ruby on Rails] Use the resources method to automatically create routes.
How to find the cause of the Ruby error
How to use the camera module OV7725 (ESP32-WROVER-B)
[Java] How to use Thread.sleep to pause the program
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use collection_select
How to use Twitter4J
When you want to use the method outside
How to use active_hash! !!
How to use MapStruct
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 Dozer.mapper
How to use Gradle
How to build the simplest blockchain in Ruby
How to use org.immutables
How to use VisualVM
How to use Map
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!