2016-05-27 52 views
-5

刪除「\」是新來ruby.I有像我如何從字符串

string = "You have successfully placed Service Request No. \#{@service_requests.id} for \#{@service_requests.category.name} . Our representative will be in touch with you soon for the same. Thank you." 

一個字符串,我想從整個字符串中刪除「\」。

output_string = "You have successfully placed Service Request No. #{@service_requests.id} for #{@service_requests.category.name} . Our representative will be in touch with you soon for the same. Thank you." 

如何使它成爲可能。

+3

對於「_how從string_中刪除反斜槓」這個問題,你給了一個正確的答案,並且你在它下面評論爲「它不工作」。這意味着,你說錯了問題。請詳細描述你有什麼,你需要做什麼,你正在屈服什麼以及有什麼問題。與此同時downvoted。 – mudasobwa

+0

其實我想從字符串中刪除反斜線。下面的一些建議不適合我。但@Зелёный答案正常工作,所以我upvoted – vicky

+0

'「\#」'不是一個反斜槓後面跟一個英鎊符號。相反,它是一個單個字符,一個轉義的'「#」',它與一個非轉義的'「#」'相同:''\#「。size#=> 1; 「\#」==「#」#=> true'。 –

回答

3
>> string = "You have successfully placed Service Request No. \#{@service_requests.id} for \#{@service_requests.category.name} . Our representative will be in touch with you soon for 
the same. Thank you." 
>> puts(string) 
=> You have successfully placed Service Request No. #{@service_requests.id} for #{@service_requests.category.name} . Our representative will be in touch with you soon for the same. Thank you. 
>> puts(string.inspect) 
=> "You have successfully placed Service Request No. \#{@service_requests.id} for \#{@service_requests.category.name} . Our representative will be in touch with you soon for the same. Thank you." 

確保你知道字符串表示(puts string.inspect)和字符串內容(puts string)之間的區別是什麼,並將反斜槓記錄爲表示的工件。

0

您可以刪除此類似:

string.gsub(%r{\"}, '') 
+0

它不工作。輸出按原樣。 「您已成功爲服務請求編號\#{@ service_requests.id}申請\#{@ service_requests.category.name},我們的代表將很快與您聯繫,謝謝。」 – vicky

+0

這對我有效 – Sunny

+0

「\」不會從輸出中刪除。 – vicky

3

試試這個:

string.gsub(/\\/, '') # or string.gsub!(/\\/, '') for inplace substitution 
+0

這是返回一個枚舉器,您缺少第二個參數。 –

+0

謝謝你指出。 –

+0

這不工作 – vicky