簡體   English   中英

刪除Ruby中的換行符

[英]Remove newlines in Ruby

有沒有辦法在多行而不是一行中編寫一個非常長的xml請求? 當我以多行寫入時,由於插入了換行符,因此不接受該字符串。 如何在文本編輯器中添加換行符並在實際程序中添加換行符? 我正在使用崇高。

紅寶石

str = "this is \n multi-line \n text \n\n"
puts str 
# this is
# multi-line
# text

使用gsub! 修改字符串

str.gsub!("\n","") 
puts str # this is  multi-line  text

軌道

使用擠壓去除所有不必要的空白區域

str = "this is \nmulti-line \n text \n\n"
puts str.squish # this is multi-line text

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM