I am reading a file in ruby by below code
completeLine = ""
file.each_line do |line|
completeLine = completeLine + line
end
now when i print completeLine variable its show its content in multipal line like
<obj id=\"pluvi\" border=\"0\"
clsid=\"clsid:VI\"
style=\"opacity: 0.0; background-color: #000000; width: 0px; height: 0px;\"></obj>"
i want it in 1 line only like
<obj id=\"pluvi\" border=\"0\" clsid=\"clsid:VI\" style=\"opacity: 0.0; background-color: #000000; width: 0px; height: 0px;\"></obj>"
so i use gsub method to remove new line character like
line = line.gsub(" *\n+", ' ')
line = line.gsub(" *\t+", ' ')
but again after this i am not able to get content in 1 line.
to see the content present in file i used below method and got ouptput like this in irb
text = File.open('G:/index.html').read
=> "<obj id=\"pluvi\" border=\"0\"\n\tclsid=\"clsid:VI\"\n\tstyle=\"opacity: 0.0; background-color: #000000; width: 0px; height: 0px;\"></obj>"
So how can i solve above problem?
You can remove all the empty/white space in the line using strip
completeLine = ""
file.each_line do |line|
completeLine = completeLine + line.strip
end
chomp will remove \\n, \\r, and \\r\\n, if $/ has not been changed from the default Ruby record separator:)
completeLine = ""
file.each_line do |line|
completeLine = completeLine + line.chomp
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.