String processing for Ruby

Source: Internet
Author: User

Ruby handles strings like numbers. We enclose them in single quotes (' ... ') or double quotes ("...").

Ruby> "abc"
"abc"
ruby> ' abc '
"abc"

Single and double quotes have different effects in some cases. A string enclosed in double quotes allows characters to be drawn by a forward slash, and can be inline with #{} expressions.

Strings that are enclosed in single quotes do not interpret strings; What you see is what it is. Several examples:

ruby> print "a\nb\nc","\n"
a

c
nil
ruby> print 'a\nb\n',"\n"
a\nb\nc
nil
ruby> "\n"
"\n"
ruby> '\n'
"\\n"
ruby> "\001"
"\001"
ruby> '\001'
"\\001"
ruby> "abcd #{5*3} efg"
"abcd 15 efg"
ruby> var = " abc "
" abc "
ruby> "1234#{var}5678"
"1234 abc 5678"

Ruby's string manipulation is more dexterous and intuitive than C. For example, you can concatenate a few with + and repeat a string several times:

Ruby> "foo" + "Bar"
"Foobar"
ruby> "foo" * 2
"Foofoo"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.