[Ruby] I want to make an array from a character string with the split method. And vice versa.

What kind of writing will give the expected value when the pattern is to arrange the character strings and make the array into a character string?

So, let's organize it with some bad patterns!

Make a string an array

We will use the split method.

** split English translation: split ** In other words, it is a method that divides a cohesive object (character string, etc.) into elements. (Arrangement) The argument is * an image that specifies the boundary of the part to be cut *

I tried to arrange from various cohesive patterns. The array you want to have is [" foo "," bar "," baz "].

Now, which writing style is right for you?

#Arrangement#Return value
"foo bar baz".split           ["foo", "bar", "baz"]  # ○
"foo bar baz".split('')	      ["f", "o", "o", " ", "b", "a", "r", " ", "b", "a", "z"]
"foo bar baz".split(',')      ["foo bar baz"]

"foobarbaz".split             ["foobarbaz"]
"foobarbaz".split('')         ["f", "o", "o", "b", "a", "r", "b", "a", "z"]
"foobarbaz".split(',')        ["foobarbaz"]
"fooxbarxbaz".split('x')      ["foo", "bar", "baz"]  # ○

"foo, bar, baz".split         ["foo,", "bar,", "baz"]
"foo, bar, baz".split('')     ["f", "o", "o", ",", " ", "b", "a", "r", ",", " ", "b", "a", "z"]
"foo, bar, baz".split(',')    ["foo", " bar", " baz"]

"foo,bar,baz".split           ["foo,bar,baz"]
"foo,bar,baz".split('')       ["f", "o", "o", ",", "b", "a", "r", ",", "b", "a", "z"]
"foo,bar,baz".split(',')      ["foo", "bar", "baz"]  # ○

%w[foo bar baz]               ["foo", "bar", "baz"]  # ○

From this, there are * 4 ways * of how to write the expected array.

I want to change from an array to a character string

We will use the join method. It will be an image of inserting the value to be inserted between * elements in the argument *.

#Stringification#Return value
["foo", "bar", "baz"].join        "foobarbaz"
["foo", "bar", "baz"].join('')    "foobarbaz"
["foo", "bar", "baz"].join(',')	  "foo, bar, baz"

Extra range array

#Arrange the range#Return value
(0..9).to_a                 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
('a'..'z').to_a             ["a",..,"z"]
(1..5).map{ |i| i**2 )      [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Recommended Posts

[Ruby] I want to make an array from a character string with the split method. And vice versa.
Rails6 I want to make an array of values with a check box
I want to output the character number from the left where an arbitrary character string appears.
I want to call a method and count the number
[Ruby] I want to put an array in a variable. I want to convert to an array
[Ruby] I want to output only the odd-numbered characters in the character string
Invoke the character string passed as an argument as a method with send
I want to ForEach an array with a Lambda expression in Java
I want to find out what character the character string appears from the left
[Ruby] I want to do a method jump!
I want to download a file on the Internet using Ruby and save it locally (with caution)
If it is Ruby, it is efficient to make it a method and stock the processing.
I tried to get the distance from the address string to the nearest station with ruby
How to make a judgment method to search for an arbitrary character in an array
How to change a string in an array to a number in Ruby
Set the date and time from the character string with POI
Learning Ruby with AtCoder 13 How to make a two-dimensional array
I want to set the video playback time (HH: MM: SS) to seconds, and vice versa.
I want to return a type different from the input element with Java8 StreamAPI reduce ()
I want to make a button with a line break with link_to [Note]
I want to convert an array to Active Record Relation with Rails
I want to return to the previous screen with kotlin and java!
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
[Java] I tried to make a maze by the digging method ♪
I want to extract between character strings with a regular expression
About the method to convert a character string to an integer / decimal number (cast data) in Java
[Java] How to search for a value in an array (or list) with the contains method
I tried to create an API to get data from a spreadsheet in Ruby (with service account)
I want to make an ios.android app
[Ruby] From the basics to the inject method
[Ruby] I want to extract only the value of the hash and only the key
I want to pass the argument of Annotation and the argument of the calling method to aspect
If you want to make a zip file with Ruby, it's rubyzip.
How to make an app with a plugin mechanism [C # and Java]
I want to get a list of only unique character strings by excluding fixed character strings from the file name
I tried to make a program that searches for the target class from the process that is overloaded with Java
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
When reassigning to an argument in a Ruby method and then calling `super` → The reassigned one is used
I want to control the start / stop of servers and databases with Alexa
[Java] Program example to get the maximum and minimum values from an array
I want to recursively get the superclass and interface of a certain class
[JDBC ③] I tried to input from the main method using placeholders and arguments.
Convert a string to a character-by-character array with swift
I want to simply write a repeating string
I want to play with Firestore from Rails
I want to get the value in Ruby
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
Rspec: I want to test the post-execution state when I set a method on subject
How to request by passing an array to the query with HTTP Client of Ruby
# 1_JAVA I want to get the index number by specifying one character in the character string.
[Java] Cut out a part of the character string with Matcher and regular expression
[Ruby] Cut out a string using the slice method
I want to call a method of another class
[Java] I want to calculate the difference from the date
[Ruby] I made a crawler with anemone and nokogiri.
I want to dark mode with the SWT app
I want to monitor a specific file with WatchService
How to output standard from an array with forEach
[Java] How to erase a specific character from a character string
I want to transition screens with kotlin and java!
Ruby Regular Expression Extracts from a specific string to a string