[Ruby] Extract character from Ruby string slice method

3 minute read

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: Extract the second letter "e" from the beginning of the string "Hello world"
string = "Hello world"
string.slice(1) #=> "e"

Returns nil if the character corresponding to the # argument is not found
string.slice(11) #=> nil

How to use the # slice method In the slice method, specify the condition of the extracted character as an argument. There are the following methods to specify the arguments.

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

Specify the number of characters from the beginning of the character string. Note that the numbers start from 0 (the first character is 0, the second from the beginning is 1)

string = "Hello world"

# Specify the number of characters from the beginning, the number at the beginning 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 and the second character after is counted as -2.

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 beginning or end of the character string and the number of characters from that

Extract characters by specifying the 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"

# Extract 5 characters from the beginning (0th)
string.slice(0,5) #=> "Hello"

# Extract the 5th character from the 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. Also in this case, the last character is counted as -1, the second character from the rear is -2, and so on.

string = "Hello world"

#-11-5 character extraction
string.slice(-11,5) #=> "Hello"

#-Extract 5 characters from the 5th
string.slice(-5,5) #=> "world"

Directly specify the character you want to extract

Extract by specifying a specific character.

string = "Hello world"

# Specify "H"
string.slice("H") #=> "H"

# Specify "Hello"
string.slice("Hello") #=> "Hello"

# Returns nil if a character not included is specified
string.slice("hello") #=> nil

Specify with regular expression

Specify the character you want to extract with a regular expression.

string = "Hello world"

# Specify "letter starting with w and ending with d" in regular expression
string.slice(/w.*d/) #=> "world"

Specify range with range operator

Use the range operator (.., …) to specify a range within a string and extract the characters. Again, the numbers start at 0.

string = "Hello world"

# Extract from the first (0th) to the 4th
string.slice(0..4) #=> "Hello"

#... does not include the characters 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 a character from a character string, similar to the slice method. The argument specification method is the same as the slice method. However, using the slice! method, the extracted characters are removed from the original string.

string = "Hello world"

Original string does not change even if you use # slice method
string.slice(0..4) #=> "Hello"
string #=> "Hello world"

Using the # slice! method will remove the string from the original string
string.slice!(0..4) #=> "Hello"
string #=> "world"

Summary

  • slice method is a method to extract characters from a string
  • Extract characters according to the conditions specified in the argument
  • Argument can be specified by character order, regular expression, or range
  • slice! method strips characters from the original string

Tags:

Updated: