Ruby string processing

Source: Internet
Author: User

Ruby processes strings like numbers. We enclose them with single quotation marks ('...') or double quotation marks.

Ruby> "ABC"

"ABC"

Ruby> 'abc'

"ABC"

Single quotation marks and double quotation marks have different functions in some cases. A string enclosed by double quotation marks allows a character to be led out by a front slash, and can be embedded with a #{} expression.

Strings enclosed in single quotes do not explain the strings. What are you looking at? 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 operations are more agile and intuitive than C. For example, you can use + to concatenate several strings and use * to repeat a string several times:

Ruby> "foo" + "bar"

"Foobar"

Ruby> "foo" * 2

"Foofoo"

In contrast, in C, because precise memory management is required, concatenating strings is much more clumsy:

Char * s = malloc (strlen (S1) + strlen (S2) + 1 );

Strcpy (S, S1 );

Strcat (S, S2 );

/*...*/

Free (s );

But for Ruby, we do not need to consider the space occupation of strings, which frees us from the cumbersome memory management.

Below are some strings for processing,

Series:

Ruby> word = "FO" + "O"

"Foo"

Repeat:

Ruby> word = word * 2

"Foofoo"

Extract characters (Note: In Ruby, the characters are considered as integers ):

Ruby> word [0]

102 #102 is ASCII code of 'F'

Ruby> word [-1]

111 #111 is ASCII code of 'O'

(A negative index refers to the offset calculated from the end of the string, rather than from the string header .)

Extract substrings:

Ruby> herb = "parsley"

"Parsley"

Ruby> herb [0, 1]

"P"

Ruby> herb [-2, 2]

"Ey"

Ruby> herb [0 .. 3]

"Pars"

Ruby> herb [-5...-2]

"RSLE"

Check equal:

Ruby> "foo" = "foo"

True

Ruby> "foo" = "bar"

False

Note: In Ruby 1.0, the above results are displayed with uppercase letters.

Well, let's try these features. The following is a guessing puzzle. Maybe the word "Puzzle" is a little cool in the following ;-)

# Save this as guess. Rb

Words = ['foobar', 'baz', 'quux ']

Secret = words [rand (3)]

Print "Guess? "

While guess = stdin. Gets

Guess. Chop!

If guess = secret

Print "you win! \ N"

Break

Else

Print "sorry, you lose. \ n"

End

Print "Guess? "

End

Print "the word was", secret, ". \ n"

Now, don't worry too muchCodeDetails. The following are puzzles.ProgramA dialog to run.

% Ruby guess. Rb

Guess? Foobar

Sorry, you lose.

Guess? Quux

Sorry, you lose.

Guess? ^ D

The word was Baz.

(Considering the success rate of 1/3, maybe I should have done a little better .)

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.