Erlang Tuple to String [duplicate] - erlang

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])).

Related

How do I split a string on a certain character in lua? [duplicate]

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

extract and convert any ascii code from a string [duplicate]

This question already has answers here:
How do I encode/decode HTML entities in Ruby?
(8 answers)
Closed 6 years ago.
How can I extract the ascii code from a string and then convert it to char?
string = "a – b"
to
a – b
Use CGI library from ruby
CGI.unescapeHTML("a – b")
#=> "a – b"

How do you confirm a string only contains numbers in Swift? [duplicate]

This question already has answers here:
Finding out whether a string is numeric or not
(18 answers)
Closed 7 years ago.
How can I check, if searchView contains just numbers?
I found this:
if newText.isMatchedByRegex("^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$") { ... }
but it checks if text contains any number. How can I do, that if all text contains just numbers in Swift?
Here is the solution you can get all digits from String.
Swift 3.0 :
let testString = "asdfsdsds12345gdssdsasdf"
let phone = testString.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
print(phone)
you can use "^[0-9]+$" instade "^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$"
This will accept one or more digits, if you want to accept only one digit then remove +

Rails, select last n characters from a string? [duplicate]

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.

Array to single string [duplicate]

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

Resources