This question already has answers here:
Array.join("\n") not the way to join with a newline?
(7 answers)
Closed 5 years ago.
I'm having an issue which I can't seem to solve. I have an array which I need to convert to an single string. The elements need to be put underneath each other.
sample_array = ['a','b','c','d','e']
desired output:
sample_array = "a
b
c
d
e"
I thought I could do this with a 'heredoc', but I can only get the elements behind each other inline. This is unfortunately not what I need. Anyone who can help me?
edit for edit question
In a single line, you can use inject:
sample_array = ['a','b','c','d','e']
puts sample_array.inject(""){|conc,x| conc + "\n" + x }
=> "a b c d e"
that will fold the array recursively and adding a line between chars
Related
This question already has answers here:
Split string in Lua?
(18 answers)
Closed 2 years ago.
I'm trying to split a string on a space in Lua. What I mean is if i had a string named "str" and it was equal to "hello world",
str = "hello world"
it should somehow return the string "world" because it is after the space. How do I do this?
if you want to take only the first value before the first space:
local result = str:match("%w+")
print(result)
if you want to collect each of the elements separated by space:
local tb = {}
for i in str:gmatch("%w+") do
print(i)
table.insert(tb, i) -- in case you want to store each separate element in a table
end
This question already has answers here:
Extracting the last n characters from a ruby string
(9 answers)
Closed 7 years ago.
I have a string "foo bar man chu" and I want to select the last 5 characters from it.
The regex expression /.{5}$/ does the job of selecting them, but how do I save them to a string in Rails? gsub(/.{5}$/,'') removes them, kind of the opposite of what I want. Thanks!
The match method will return the result of attempting to match the string with the regular expression
result = "foo bar man chu".match(/.{5}$/)
puts result
=> "n chu"
If the regular expression is not matched, then nil will be returned.
This question already has answers here:
How to modify user input?
(4 answers)
Closed 9 years ago.
I would like to replace text in an user input with an variable.
I wrote an little demo code, to show you my problem:
puts "Enter your feeling"
a = gets.chomp
#feel = "good"
puts a
SO when it comes to the input, i type in:
Actually i fell very #{#feel}
Then i hope to get this output:
Actually i fell very good
But instead i get this output:
Actually i fell very #{#feel}
What did i make wrong?
You can make use of Kernal#eval
eval ("a")
#{variable} works only double quotes (" "). So you try inside of "". if you use this ' ', it will consider as string
This question already has answers here:
Convert erlang terms to string, or decode erlang binary
(2 answers)
Closed 9 years ago.
Is there a way how to convert a tuple to a string ?
Consider I have the following list :
[{atom,5,program},{atom,5,receiving},{nil,5}]
I wish to convert this into the following string:
"{atom,5,program},{atom,5,receiving},{nil,5}"
I have tried using erlang:tuple_to_list on each element in the list, which returns
A = [atom,5,program]
Eventually, I can't concatenate that with "{" ++ A ++ "}"
Any ideas how I can turn that to a string ?
Term = [{atom,5,program},{atom,5,receiving},{nil,5}].
lists:flatten(io_lib:format("~p", [Term])).
This question already has answers here:
Ruby: Split string at character, counting from the right side
(6 answers)
Closed 9 years ago.
I want to be able to split strings into 2 elements, because each string would contain at least one delimiter.
Example: "hello_world". If I apply .split("_") then I receive: ["hello", "world"].
The problem arises when I have a string with two or more delimiters. Example "hello_to_you".
I want to receive: ["hello_to", "you"].
I know about the limit option for the split function: .split("_", 2), but it produces: ["hello", "to_you"].
So, basically, I would need to split the entire string ONLY with the last delimiter ("_").
This is exactly what String#rpartition does:
first_part, _, last_part = 'hello_to_you'.rpartition('_')
first_part # => 'hello_to'
last_part # => 'you'
try
'hello_to_you'.split /\_(?=[^_]*$)/
class String
def split_by_last_occurrance(char=" ")
loc = self.rindex(char)
loc != nil ? [self[0...loc], self[loc+1..-1]] : [self]
end
end
"test by last_occurrance".split_by_last #=> ["test by", "last"]
"test".split_by_last_occurrance #=> ["test"]