Home > Back-end >  How to remove the commas from an array of strings
How to remove the commas from an array of strings

Time:05-25

I have a array of string arry = [ "Laptop", "mobile", "car"]

i want to make this arry = ["laptop" "mobile" "car"]

i tried it in ruby. but it doesn't matter any language as far as i can get the output in this format.

CodePudding user response:

There are no commas in any of your values, so no matter what you do you can't remove them. The commas are instead part of the language's default formatting when turning arrays into strings.

Instead of relying on that, turn the array into a string yourself:

arry = [ "Laptop", "mobile", "car"]
s = "["   arry.map { |x| x.downcase.dump }.join(" ")   "]"
print s

This outputs ["laptop" "mobile" "car"]

CodePudding user response:

Is this an output requirement or you will be using this array for another purpose. If you want to access the elements then you need "," so that indexing can be done. This is a default functionality. However manipulating array values for display should be straight-forward.

  • Related