Common functions of the "Ruby Learning Note" string

Source: Internet
Author: User
Tags chop

String is the most commonly used class in almost all object-oriented programming languages, and is no exception in Ruby, which is documented in several common methods of string in Ruby.


Concat: Connect 2 strings, sample:

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >puts "Hello". Concat ("world!") </span>

Output: Hello world!


Capitalize: Even the first letter uppercase, the other letter lowercase, example:

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >puts "Hello world!". Capitalize () </span>

Output: Hello world!


Downcase: Convert to lowercase, sample:

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >puts "Hello world!". Downcase () </span>

Output: Hello world!


UpCase: Capitalization, example:

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >puts "Hello world!". UpCase () </span>

Output: HELLO world!


Swapcase: Case reversal, Sample:

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >puts "Hello". Swapcase () </span>

Output: HELLO


Chop: Delete the character at the end, sample:

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >puts "Hello World". Chop () </span>

Output: Hello worl


Hash: Get hash value, sample:

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >puts "Hello world!". Hash () </span>

Output: 224243329


Next: The Last word Fugai, example:

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >puts "Hello World". Next () </span>

Output: Hello Worle


Reverse: Reverse order, Sample:

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >puts "Hello". Reverse () </span>

Output: Olleh


Sum: Sum the values corresponding to each character, for example:

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >puts "Hello". Sum () </span>

Output: 532



Common functions of the "Ruby Learning Note" string

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.