End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
StringScanner#charpos
charpos
ext/strscan/strscan.c
View on GitHub ↗
static VALUE
strscan_get_charpos(VALUE self)
{
struct strscanner *p;
GET_SCANNER(self, p);
return LONG2NUM(rb_enc_strlen(S_PBEG(p), CURPTR(p), rb_enc_get(p->str)));
}
Returns the character position of the scan pointer. In the ‘reset’ position, this value is zero. In the ‘terminated’ position (i.e. the string is exhausted), this value is the size of the string.
In short, it’s a 0-based index into the string.
Example
s = StringScanner.new("abcädeföghi") s.charpos # -> 0 s.scan_until(/ä/) # -> "abcä" s.pos # -> 5 s.charpos # -> 4
See also
#captures
#check
#beginning_of_line?
#check_until
also in
4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1