「Ruby」rindexメソッドで検索開始位置を指定して文字列後ろから検索する

環境
Windows 10 Home 64bit
ruby 3.1.2p20

構文
文字列.rindex(検索する文字,開始位置)
引数1は、検索する文字列です
引数2は、検索を開始する位置です。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
tt = "テほかくテほかく"
puts tt.rindex("テ", 5)
puts tt.rindex("テ", 4)
puts tt.rindex("テ", 3)
puts tt.rindex("テ", 2)
puts tt.rindex("テ", 1)
puts tt.rindex("テ", 0)
tt = "テほかくテほかく" puts tt.rindex("テ", 5) puts tt.rindex("テ", 4) puts tt.rindex("テ", 3) puts tt.rindex("テ", 2) puts tt.rindex("テ", 1) puts tt.rindex("テ", 0)
tt = "テほかくテほかく"
puts tt.rindex("テ", 5) 
puts tt.rindex("テ", 4) 
puts tt.rindex("テ", 3) 
puts tt.rindex("テ", 2) 
puts tt.rindex("テ", 1) 
puts tt.rindex("テ", 0)

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>ruby test.rb
4
4
0
0
0
0
>ruby test.rb 4 4 0 0 0 0
>ruby test.rb
4
4
0
0
0
0

 

Ruby

Posted by arkgame