Ruby String Processing _ruby topics

Source: Internet
Author: User
Tags strlen
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 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"


In C, in contrast, concatenated strings are much more clumsy because of the need for precise memory management:

Char *s = malloc (strlen (S1) +strlen (S2) +1);
strcpy (S, s1);
strcat (s, S2);
/* ... */
Free (s);


But for Ruby, we don't have to consider the space footprint of strings, which frees us from cumbersome memory management.

Here is some string processing,

Series:

Ruby> word = "fo" + "O"
"Foo"


Repeat:

Ruby> Word = Word * 2
"Foofoo"


Extract character (note: In Ruby, characters are treated as integers):

Ruby> Word[0]
102 # 102 is ASCII code of ' F '
Ruby> Word[-1]
# is ASCII code of ' O '


(The negative index is the offset from the tail of the string, not from the string header.)

Extract substring:

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 for equality:

Ruby> "foo" = "foo"
True
Ruby> "foo" = "Bar"
False


Note: In Ruby 1.0, the above results appear in uppercase letters.

OK, let's try these features. Here is a riddle of guessing words, maybe the word "riddle" is so cool in the following things;--)

# 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 much about the code details. Here is a dialog for the puzzle program to run.

% Ruby Guess.rb
Guess? Foobar
Sorry, you lose.
Guess? Quux
Sorry, you lose.
Guess? ^d
The word was Baz.


(Given the 1/3 success rate, 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.