I want to have a user type some words, and get them converted to an array. I want to split by word. Here is an example:
"15 17 21 46"
→ [15, 17, 21, 46]
Stuff like split("")
and scan
make an array out of every single letter.
I would prefer doing it without using YAML. Also, take note in the example that it doesn't include the spaces but it uses those as breaks between values.
Is it possible to do what I did in the example above where I have a bunch of numbers with spaces and those get converted directly to integers? I feel like the spaces might interrupt it in case I do something like to_i
.
This question is similar to String to Array in Ruby, but it does not have an answer.
Python String split() Method Python string method split() returns a list of all the words in the string, using str as the separator (splits on all whitespace if left unspecified), optionally limiting the number of splits to num.
The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
C# allows to split a string by using multiple separators. var text = "falcon;eagle,forest,sky;cloud,water,rock;wind"; var words = text. Split(new char[] {',', ';'}); Array. ForEach(words, Console.
String#split
doesn't actually need an argument if you want to split around whitespaces :
"15 17 21 46".split
#=> ["15", "17", "21", "46"]
If you want to specify an argument, you need to use a space, not an empty string :
"15 17 21 46".split(' ')
#=> ["15", "17", "21", "46"]
And if you want to convert those strings to integers :
"15 17 21 46".split(' ').map(&:to_i)
#=> [15, 17, 21, 46]
Eric Duminil's answer shows how to split the string on what the contents is not -- spaces. Perfectly valid if you know that your string contains only digits of interest and spaces.
You can also gather the elements based on what you want -- digits vs what you don't want -- the spaces between the digits.
To do that in Ruby, use a regex with .scan
:
> "15 17 21 46".scan(/\d+/)
=> ["15", "17", "21", "46"]
Or, if you have numbers embedded in strings that you do not want:
> "15 17 21 46 123abc 34".scan(/\b\d+\b/)
=> ["15", "17", "21", "46", "34"]
Note that 123
is not included since it is part of the string 123abc
as opposed to:
> "15 17 21 46 123abc 34".scan(/\d+/)
=> ["15", "17", "21", "46", "123", "34"]
This includes 123
since the word break \b
was not part of the regex.
If you just split on spaces:
> "15 17 21 46 123abc 34".split
=> ["15", "17", "21", "46", "123abc", "34"]
What the 'right answer' is is based on your goal and your data.
Then you can convert those values to integers:
"15 17 21 46 123abc 34".scan(/\b\d+\b/).map {|s| Integer(s) }
=> [15, 17, 21, 46, 34]
Note that I am calling Integer
vs the more permissive .to_i
. Integer
will raise an exception for strings that are not completely converted to an integer vs to_i
that will convert the first few and silently ignore anything else:
> '1.5E+06'.to_i
=> 1
> Integer('1.5E+06')
ArgumentError: invalid value for Integer(): "1.5E+06"
from (irb):19:in `Integer'
from (irb):19
from /usr/local/bin/irb:11:in `<main>'
Again, it depends on the strings and your goal to know which is 'right.'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With