1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Everything About Strings


Everything About Strings

Strings are one of the base types of most programming languages. Though computers exist as entities of pure mathematics, dealing only with numbers, at some point they're going to have to either process or display text eventually destined for a human being. And for that reason, you need strings. Strings are variables that hold strings (hence the name) of characters, these can be of any length and can contain any characters. For example, the string "Hello world" is a common programming language greeting, or the player of a game might input their name, and that'll be stored in a string, or you download the contents of a webpage and store it in memory, in a string. Strings are everywhere, even in purely mathematical programs you will be dealing with strings at some point in time.

Below are a list of articles you might find useful if you want to learn about strings. They're ordered (roughly) in least to most demanding in terms of programming and Ruby knowledge. If you're new to programming, pay close attention to regular expressions. At first they seem a bit useless and esoteric, but they are one of the most useful features of high level programming languages, and you'll find yourself using them often.

  • String literals are string that originate in your program itself. They're embedded within the program right along with all the method calls and such. Almost all useful programs have a few of these, if only to print some debug information. If you're familiar with string literals in other programming languages, you'll be familiar with them in Ruby, however there are a few catches, and a few extra features available to you when using Ruby.
  • Getting and Printing Strings is one of the first things you'll learn in most any programming language. From the obligatory "hello world" program to a simple number guessing game, the level of immediacy and instant gratification that getting and printing strings help you explore a programming language quickly. Oh, and they're useful in console programs, or in file access, and... well, everywhere else really.
  • Indexing Strings is a common subject, especially in text processing programs. Many tasks boil down to text processing, taking text as you input, separating out the parts you want, doing something with those (such as forming an HTTP query, database query, tabulation and accounting, or just producing text of another format) strings and printing a result (that may be consumed by another program, or a human). Ruby has you covered here, with many methods for processing text. Among them, indexing strings, the most esoteric (thing indexing C strings), but with a few modern twists. It may not be fancy, but sometimes all you need is a hammer.
  • Splitting Strings is another very useful string operation. Take a string and split it on the spaces into multiple strings. Or on commas, or on any other text you can describe. What you do with this split data is up to you, maybe the data is from a CSV file and you need to do some tabulation? Or maybe it was a list of names and emails and you need the program to send email to these people? It's up to you, but it all starts with splitting the string.
  • String Substitution is taking one part of a string and replacing it with another part. This type of operation is often a "glue" operation, something that fixes up data so it looks how another method expects. It involves taking a string, a sub-string to match (usually using a regular expression), and either replacing it with a static string or passing it to a block to be replaced there. In Ruby, this is achieved with the sub and gsub methods, available to all string objects.
  • Partitioning Strings is a subject related to splitting strings, but with a twist. Instead of consuming the string you wish to split on, it's preserved. This has a distinct advantage of being non-destructive, so it can be used on strings where every byte counts, none of it is "empty" separating whitespace.

So there you have it, virtually everything you might want to know about strings in Ruby. There is more, of course, but these are the major points. With a concept as universal as a string, the method list just goes on and on, but for most of your Ruby career, you won't use but a few methods from the string class.

See More About
  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. Strings
  6. Everything About Strings

©2014 About.com. All rights reserved.