<< Back to man.ChinaUnix.net

Valid XHTML 1.0! Valid CSS!

Strings

So that's numbers... but what about letters? Words? Text?

The word we use for groups of letters is string. Here are some strings:

Some things you can do with strings

Ruby lets you do several nifty things with strings.

Here is a good way to remember this: '"hi " * 3' is the same as '"hi " + "hi " + "hi "' so it gives me "hi hi hi ".

Notice what happens if I forget the space in "hi ":

Now see what happens here:

When Ruby sees "1" it sees a string, not a number.

More things you can do with text

Here are some more nifty things you can do with text:

Exercises

  1. What do you expect this will do:

    >> 'hello'.length + 'world'.length
    

    Try it.

  2. Play around with strings for a bit.