Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

StringScanner#charpos

() -> ::Integer
charpos

call-seq: charpos -> character_position

Returns the character position (initially zero), which may be different from the byte position given by method pos:

Example
scanner = StringScanner.new(HIRAGANA_TEXT)
scanner.string # => "こんにちは"
scanner.getch  # => "こ" # 3-byte character.
scanner.getch  # => "ん" # 3-byte character.
put_situation(scanner)
# Situation:
#   pos:       6
#   charpos:   2
#   rest:      "にちは"
#   rest_size: 9
See also #captures #check #beginning_of_line? #check_until also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0