Ruby class String interactive
→ Click to get an interactive sample (type in code)
! → Has also a bang method (modifies self)
Creating
# str = 'Dig it'
try_convertString.try_convert(42) # nil (String if it responds to to_str) Documentation Playground
["Dig it", nil]Querying
Counts
Substrings
Encodings
Other
Comparing
Modifying (modifies self)
Insertion
Substitution
! See non-bang methods in 'Converting to New String' for:
sub! gsub! succ! reverse! tr! tr_s!
Casing
! See non-bang methods in 'Converting to New String' for:
capitalize! downcase! upcase! swapcase!
Encoding
! See non-bang methods in 'Converting to New String' for:
encode! unicode_normalize! scrub!
Deletion
! See non-bang methods in 'Converting to New String' for:
delete! delete_prefix! delete_suffix! strip! lstrip! rstrip! chomp! chop! squeeze! slice!
Converting to New String
Extension
Encoding
Substitution
Casing
Deletion
Duplication
Converting to Non-String
Characters, Bytes, and Clusters
Splitting
Matching
Numerics
Strings and Symbols
Iterating
# a=[]; s='ok\u0301' (é as e + combining accent)
each_bytes.each_byte{|b|a<<b} # [111,107,101,204,129] Documentation Playground
[111, 107, 101, 204, 129, 33]each_chars.each_char{|b|a<<b} # ['o','k','e','́'] Documentation Playground
["o", "k", "e", "́", "!"]each_codepoints.each_codepoint{|b|a<<b} # [111,107,101,769] Documentation Playground
[111, 107, 101, 769, 33]each_grapheme_clusters.each_grapheme_cluster{…} # ['o','k','é'] (e + accent = 1 cluster) Documentation Playground
["o", "k", "é", "!"]