Extract characters from Ruby strings slice method

What is the slice method?

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

How to use the slice method

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 or end of the character string

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"

Specify the number from the beginning or the end of the character string and the number of characters from there

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"

Directly specify the character you want to extract

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

Specified by regular expression

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"

Specify a range with the range operator

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"

Difference between slice method and slice! method

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"

Summary

Recommended Posts

Extract characters from Ruby strings slice method
[Ruby] slice method
[Java] Remove whitespace from character strings
Extract characters from Ruby strings slice method
[Ruby] Method to count specific characters
[Ruby] From the basics to the inject method
Ruby to_s method
[Ruby] Method memorandum
[Ruby] initialize method
Ruby build method
Ruby accessor method
ruby map method
[Ruby basics] How to use the slice method
[Ruby] Cut out a string using the slice method
Ruby Learning # 30 Initialize Method
abbreviation for ruby method
Ruby Learning # 24 Exponent Method
Ruby Thread # [] = method notes
definition of ruby method
From Java to Ruby !!
[Ruby] Method definition summary