String tools

Contents
Split and join
sub/gsub and replace
Regexp match
sprintf

Split and join

The method split cuts a string into an array around a separator. Without arguments it splits on whitespace, which tidies messy input. The method join performs the reverse trip, gluing an array back together with a separator between elements.

words = "Ada,Bob,Lin".split(",") puts words.length puts words.join(" and ")

3
Ada and Bob and Lin

sub/gsub and replace

The method sub replaces the first match, while gsub replaces every match in the whole string. Both build a fresh string and leave the original untouched. Bang variants ending with an exclamation mark rewrite the string in place instead.

s = "aaa" puts s.sub("a", "o") puts s.gsub("a", "o") puts s

oaa
ooo
aaa

Regexp match

The operator =~ returns the index of the first match, or nil when nothing matches. The method match returns a MatchData object, with captures available in brackets. The predicate match? gives a plain boolean when the captures are not needed.

s = "age 36" puts(s =~ /[0-9]+/) m = s.match(/([0-9]+)/) puts m[1] puts s.match?(/[0-9]+/)

4
36
true

The digits start at index 4, and the first capture group holds 36.

sprintf

The methods format and sprintf fill a template with values: %d takes an integer, %s takes a string, and %.2f rounds a float to two decimals. Templates keep padding and precision in one readable place.

puts format("%d plus %d is %d", 2, 3, 5) puts sprintf("%.2f", 3.14159)

2 plus 3 is 5
3.14

Next: Files and IO

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: