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


Ruby String each_str用法及代碼示例

each_str是Ruby中的String類方法,用於將給定字符串中的每個字符傳遞給給定的塊,或者如果沒有給出塊,則返回一個枚舉器。

用法: str.each_byte

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


返回值:枚舉器。

示例1:

# Ruby program to demonstrate  
# the each_char method  
       
# Taking a string and  
# using the method 
puts "Ruby".each_char{|b| print b, ' ' } 
puts "Programming".each_char{|b| print b, ' ' }

輸出:

R u b y Ruby
P r o g r a m m i n g Programming

示例2:

# Ruby program to demonstrate  
# the each_char method  
       
# Taking a string and  
# using the method 
puts "Sample".each_char{|b| print b, ' ' } 
puts "Input".each_char

輸出:

S a m p l e Sample
#


相關用法


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