當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby String encoding用法及代碼示例

encoding是Ruby中的String類方法,用於返回表示對象編碼的Encoding對象。

用法: str.encoding

參數:在這裏,str是給定的字符串。


返回值:編碼對象。

示例1:

# Ruby program to demonstrate  
# the encoding method  
       
# Taking a string and  
# using the method 
puts "R\xC3\xA9sum\xC3\xA9".encoding 
puts "Ruby\n\n".encoding

輸出:

UTF-8
UTF-8

示例2:

# Ruby program to demonstrate  
# the encoding method  
       
# Taking a string and  
# using the method 
puts "Sample".encoding 
puts "Input\n\n".encoding

輸出:

UTF-8
UTF-8


相關用法


注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 Ruby | String encoding Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。