Chautauqua Logo

Ruby class String interactive

Prompt icon → Click to get an interactive sample (type in code)
! → Has also a bang method (modifies self)

Creating

# str = 'Dig it'
newstr = String.new('Dig it') # Dig it Documentation Interactive samplePlayground
Dig it

Querying

Counts

length (alias size)str.length # 6 Documentation Interactive samplePlayground
6
bytesizestr.bytesize # 6 Documentation
countstr.count('i') # 2 Documentation Interactive samplePlayground
1
empty?str.empty? # false Documentation Interactive samplePlayground
false

Substrings

=~str =~ /g/ # 2 Documentation Interactive samplePlayground
2
indexstr.index('i') # 1 Documentation Interactive samplePlayground
1
rindexstr.rindex('i') # 4 Documentation Interactive samplePlayground
4
byteindexstr.byteindex('i') # 1 Documentation Interactive samplePlayground
1
byterindexstr.byterindex('i') # 4 Documentation Interactive samplePlayground
4
include?str.include?('ig') # true Documentation Interactive samplePlayground
true
matchstr.match('i') # i Documentation Interactive samplePlayground
i
match?str.match?(/i/) # true Documentation Interactive samplePlayground
true
start_with?str.start_with?('Di') # true Documentation Interactive samplePlayground
true
end_with?str.end_with?('it') # true Documentation Interactive samplePlayground
true

Encodings

encodingstr.encoding # #<Encoding:UTF-8> Documentation Interactive samplePlayground
UTF-8
unicode_normalized?"\u00E0".unicode_normalized? # true Documentation Interactive samplePlayground
true
valid_encoding?"Mañana".valid_encoding? # true Documentation Interactive samplePlayground
true
ascii_only?"Mañana".ascii_only? # false Documentation Interactive samplePlayground
false

Other

sumstr.sum # 529 Documentation Interactive samplePlayground
529
hashstr.hash # 6345789 (differs) Documentation Interactive samplePlayground

Comparing

==str == 'foo' # false Documentation Interactive samplePlayground
false
eql?str.eql?('foo') # false Documentation Interactive samplePlayground
false
<=>'a'<=>'b'; 'a'<=>'a'; 'b'<=>'a' # -1;0;1 Documentation Interactive samplePlayground
-1
casecmp'a'.casecmp('B') # -1 (Ignoring case) Documentation Interactive samplePlayground
-1
casecmp?'a'.casecmp?('B') # false (Ignoring case) Documentation Interactive samplePlayground
false

Modifying (modifies self)

Insertion

insertstr.insert(4, 'more of ') # Dig more of it Documentation Interactive samplePlayground
Dig more of it
<<str << ' now'; str << 33 # Dig it now! Documentation Interactive samplePlayground
Dig more of it
append_as_bytes''.append_as_bytes(56, 33) # 8! Documentation Interactive samplePlayground
8!
prependstr.prepend('Go! ') # Go! Dig it Documentation Interactive samplePlayground
Go! Dig it

Substitution

bytesplicestr.bytesplice(0, 3, 'Make') # Make it Documentation Interactive samplePlayground
Make it
setbytestr.setbyte(2, 129) # Di\x81 it Documentation Interactive samplePlayground
Di! it
replacestr.replace('Brand new') # Brand new Documentation Interactive samplePlayground
Brand new
! 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

force_encodingstr.force_encoding('ascii') # <…:US-ASCII> Documentation Interactive samplePlayground
Make it
! See non-bang methods in 'Converting to New String' for:
encode! unicode_normalize! scrub!

Deletion

clearstr.clear # "" Documentation Interactive samplePlayground
! 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

*'Hi' * 3 # HiHiHi Documentation Interactive samplePlayground
HiHiHi
+'Hi ' + 'there' # Hi there Documentation Interactive samplePlayground
Hi there
center'Hi'.center(6) # ' Hi ' Documentation Interactive samplePlayground
--Hi--
concat'Hi'.concat(33, 'there') # Hi!there Documentation Interactive samplePlayground
Hi!there
ljust'Hi'.ljust(6) # 'Hi    ' Documentation Interactive samplePlayground
Hi----
rjust'Hi'.rjust(6, '-') # '----Hi' Documentation Interactive samplePlayground
----Hi

Encoding

bs="\x99";s.b.encoding # BINARY (ASCII-8BIT) Documentation Interactive samplePlayground
ASCII-8BIT
scrub!"foo\x81\x81bar".scrub # foo��bar Documentation Interactive samplePlayground
foo��bar
unicode_normalize!"\u00E9".unicode_normalize # é Documentation Interactive samplePlayground
é
encode!str.encode # str.encoding #<Encoding:UTF-8> Documentation Interactive samplePlayground
UTF-8

Substitution

dump'hi'.dump # "\"hi\"" Documentation Interactive samplePlayground
"\"hi\""
undump"\"hi\"".undump # "hi" Documentation Interactive samplePlayground
hi
sub!str.sub('Dig', 'Break') # Break it Documentation Interactive samplePlayground
Break it
gsub!str.gsub('i', 'o') # Dog ot Documentation Interactive samplePlayground
Dog ot
succ!str.succ # Dig iu (increment character) Documentation Interactive samplePlayground
Dig iu
reverse!str.reverse # ti giD Documentation Interactive samplePlayground
ti giD
tr!str.tr('it','ol') # Dog ol Documentation Interactive samplePlayground
Dog ol
tr_s!'Snoopy'.tr_s('o','-') # Sn-py (squeezes) Documentation Interactive samplePlayground
Sn-py
%'%04x' % 32 # "0020" Documentation Interactive samplePlayground
0020

Casing

capitalize!'nice'.capitalize # Nice Documentation Interactive samplePlayground
class
downcase!'CLASS'.downcase # class Documentation Interactive samplePlayground
class
upcase!str.upcase # DIG IT Documentation Interactive samplePlayground
DIG IT
swapcase!str.swapcase # dIG IT Documentation Interactive samplePlayground
dIG IT

Deletion

delete!str.delete('i') # 'Dg t' Documentation Interactive samplePlayground
Dg t
delete_prefix!str.delete_prefix('Di') # g it Documentation Interactive samplePlayground
g it
delete_suffix!str.delete_suffix(' it') # Dig Documentation Interactive samplePlayground
Dig
strip!' Dig it '.strip # 'Dig it' Documentation Interactive samplePlayground
"Dig it"
lstrip!' Dig it '.lstrip # 'Dig it ' Documentation Interactive samplePlayground
"Dig it "
rstrip!' Dig it '.rstrip # ' Dig it' Documentation Interactive samplePlayground
" Dig it"
chomp!"Dig it\n\n".chomp() # 'Dig it' Documentation Interactive samplePlayground
"Dig it"
chop!"Dig it\r\n".chop # 'Dig it' Documentation Interactive samplePlayground
"Dig it"
squeeze!'Diig iiit'.squeeze('i') # Dig it Documentation Interactive samplePlayground
"Dig it"
slice!str.slice(2) # g Documentation Interactive samplePlayground
g
[] (alias slice)str[2] # g Documentation Interactive samplePlayground
g
byteslicestr.byteslice(2) # g Documentation Interactive samplePlayground
g
chrstr.chr # D Documentation Interactive samplePlayground
D

Duplication

to_s (alias to_str)123.to_s # "123" Documentation Interactive samplePlayground
123

Converting to Non-String

Characters, Bytes, and Clusters

bytes'Olé'.bytes # [79,108,195,169] Documentation Interactive samplePlayground
[79, 108, 195, 169]
chars'Olé'.chars # ['O','l','é'] Documentation Interactive samplePlayground
["O", "l", "é"]
codepoints'Olé'.codepoints # [79,108,233] Documentation Interactive samplePlayground
[79, 108, 233]
getbyte'Olé'.getbyte(0) # 79 Documentation Interactive samplePlayground
79
grapheme_clusters'Olé'.grapheme_clusters # ['O','l','é'] Documentation Interactive samplePlayground
["O", "l", "é"]

Splitting

lines"L1\nL2".lines # ['L1\n','L2'] Documentation Interactive samplePlayground
["L1\n", "L2"]
partitionstr.partition(/i/) # ['D','i','g it'] Documentation Interactive samplePlayground
["D", "i", "g it"]
rpartitionstr.rpartition(/i/) # ['Dig ','i','t'] Documentation Interactive samplePlayground
["Dig ", "i", "t"]
splitstr.split(' ') # ['Dig','it'] Documentation Interactive samplePlayground
["Dig", "it"]

Matching

scanstr.scan(/w+/) # ['Dig','it'] Documentation Interactive samplePlayground
["Dig", "it"]
unpack'AB!*'.unpack('CCCC') # [65,66,33,42] Documentation Interactive samplePlayground
[65, 66, 33, 42]
unpack1'A'.unpack1('C') # 65 Documentation Interactive samplePlayground
65

Numerics

hex'ff'.hex # 255 Documentation Interactive samplePlayground
255
oct'555'.oct # 365 Documentation Interactive samplePlayground
365
ord'!'.ord # 33 Documentation Interactive samplePlayground
33
to_c'2+3i'.to_c.rect # [2,3] Documentation Interactive samplePlayground
[2, 3]
to_i'66abc'.to_i # 66 Documentation Interactive samplePlayground
66
to_f'66abc'.to_f # 66.0 Documentation Interactive samplePlayground
66.0
to_r'0.25'.to_r # (1/4) Documentation Interactive samplePlayground
(1/4)

Strings and Symbols

inspectstr.inspect # "\"Dig it\"" Documentation Interactive samplePlayground
"Dig it"
to_sym (alias intern)'dig'.to_sym # :dig Documentation Interactive samplePlayground
:dig
intern (alias to_sym)'dig'.intern # :dig Documentation Interactive samplePlayground
:dig

Iterating

# a=[]; s='oké'
each_bytes.each_byte{|b|a<<b} # [111,107,195,169] Documentation Interactive samplePlayground
[111, 107, 195, 169, 33]
each_chars.each_char{|b|a<<b} # ['o','k','e','´'] Documentation Interactive samplePlayground
["o", "k", "é", "!"]
each_codepoints.each_codepoint{|b|a<<b} # [111,107,233] Documentation Interactive samplePlayground
[111, 107, 233, 33]
each_grapheme_clusters.each_grapheme_cluster{…} # ['o','k','é'] Documentation Interactive samplePlayground
["o", "k", "é", "!"]
each_line"L1\nL2".each_line{|l|a<<l} # ['L1\n','L2'] Documentation Interactive samplePlayground
["L1\n", "L2"]
upto'a'.upto('d') # ['a','b','c','d'] Documentation Interactive samplePlayground
["a", "b", "c", "d"]