The slice method is a method that extracts the character of the condition specified by the argument from the character string.
#Example: string"Hello world"2nd character from the beginning of"e"Extract
string = "Hello world"
string.slice(1) #=> "e"
#Returns nil if no corresponding character is found in the argument
string.slice(11) #=> nil
In the slice method, specify the condition of the character to be extracted in the argument. There are the following methods to specify the argument.
Specify the number of characters from the beginning of the character string. Note that the numbers start with 0 (first letter is 0, second from the beginning is 1)
string = "Hello world"
#Specify the number of characters from the beginning, the first number is 0
string.slice(0) #=> "H"
string.slice(1) #=> "e"
string.slice(2) #=> "l"
string.slice(3) #=> "l"
string.slice(4) #=> "o"
By specifying a negative value, you can also specify the number of characters from the back. In that case, the last character is counted as -1, the second from the back is counted as -2, and so on.
string = "Hello world"
#Specify the number of characters from the back, the last number is-1
string.slice(-5) #=> "w"
string.slice(-4) #=> "o"
string.slice(-3) #=> "r"
string.slice(-2) #=> "l"
string.slice(-1) #=> "d"
Extract characters by specifying a range in the character string. Specify the starting position and how many characters to extract from it. Again, the numbers start at 0.
string = "Hello world"
#lead(0th)Extract 5 characters from
string.slice(0,5) #=> "Hello"
#Extract 5 characters from 6th
string.slice(6,5) #=> "world"
By specifying a negative value for the start position, you can also specify the number of characters from the back. In this case as well, the last character is counted as -1, and the second character from the back is counted as -2.
string = "Hello world"
# -Extract 5 characters from 11th
string.slice(-11,5) #=> "Hello"
# -Extract 5 to 5 characters
string.slice(-5,5) #=> "world"
Extract by specifying a specific character.
string = "Hello world"
# "H"Specify
string.slice("H") #=> "H"
# "Hello"Specify
string.slice("Hello") #=> "Hello"
#Returns nil if you specify a character that is not included
string.slice("hello") #=> nil
Specify the character you want to extract with a regular expression.
string = "Hello world"
#Specify "characters starting with w and ending with d" in the regular expression
string.slice(/w.*d/) #=> "world"
Use the range operators (.., ...) to specify a range within a string and extract the characters. Again, the numbers start at 0.
string = "Hello world"
#lead(0th)Extracted from to 4th
string.slice(0..4) #=> "Hello"
# ...Does not include the character at the end of the range
string.slice(0...4) #=> "Hell"
There is also a method called slice !, but this is also a method that extracts and returns characters from a character string in the same way as the slice method. The method of specifying the argument is the same as the slice method. However, if you use the slice! method, the extracted characters will be removed from the original string.
string = "Hello world"
#Using the slice method does not change the original string
string.slice(0..4) #=> "Hello"
string #=> "Hello world"
# slice!Using the method strips the string from the original string
string.slice!(0..4) #=> "Hello"
string #=> " world"
Recommended Posts