use gsub to eradicate the spaces from text string : gsub « String « Ruby
- Ruby
- String
- gsub
use gsub to eradicate the spaces from text string
lines = File.readlines("text.txt")
line_count = lines.size
text = lines.join
puts "#{line_count} lines"
total_characters_nospaces = text.gsub(/\s+/, '').length
puts "#{total_characters_nospaces} characters excluding spaces"
Related examples in the same category