+ 2

Ruby:How to delete a special string in an array

How to delete an string with there name?

23rd Aug 2017, 3:36 PM
Parthu
2 Answers
+ 4
Removing Elements Just as you can add new elements to existing arrays, you can remove elements, too. To remove the last element from the array, use pop: a = [1, 2] a.pop # a = [1] To remove an item from the beginning of the array, use shift: a = [1, 2, 3, 4] a.shift # a = [2, 3, 4] To remove an item from the middle of the array, use delete_at, providing it the index position of the item to be deleted: a = [1, 2, 3, 4, 5, 6] a.delete_at(2) # a = [1, 2, 4, 5, 6] To remove an item from the array based upon its value, use delete: pets = ['cat', 'dog', 'rabbit', 'parrot'] a.delete('dog') # pets = ['cat', 'rabbit', 'parrot'] try with gsub to delete special characters a.gsub!(/[!@%&"]/,'') try the regexp on rubular.com if you want something more general you can have a string with valid chars and remove what's not in there: a.gsub!(/[^abcdefghijklmnopqrstuvwxyz ]/,'')
23rd Aug 2017, 5:06 PM
GAWEN STEASY
GAWEN STEASY - avatar
0
Thanks :)
23rd Aug 2017, 6:14 PM
Parthu