Gigi Labs

Please follow Gigi Labs for the latest articles.

Monday, May 6, 2013

C# Basics: Working with Strings

Hey guys and girls!

In the articles so far, we used Strings, which are just variables that hold text, such as "Doh!" (always surrounded by double quotes). In this article we'll learn a bit more about what we can do with these Strings.

A String is actually made up of a series of characters (char data type), and each character has a number (called an index), starting at zero. If your String is "Hello!", then it looks like this:

You can access a particular character like this:

            String str = "Hello!";
            char secondCharacter = str[1];
            Console.WriteLine("The second character of {0} is: {1}", str, secondCharacter);


Since the character numbering starts at zero, then the second character ('e' in this case) is the one with index 1. An important difference between using Strings and characters is that when using literal values, Strings must use double quotes (as above), while characters use single quotes:

            char someCharacter = 'a';

Now, let's create a new project in SharpDevelop (or Visual Studio, if you prefer). Replace the sample code in Main() with the following:

            String str = "The quick brown fox jumps over the lazy dog.";
            Console.Write("Press any key to continue . . . ");

In the previous articles, we've been using a String containing {0} in Console.WriteLine(). If you're writing a string, you can just pass it directly to Console.WriteLine(), as we're doing above. Result:

Now I'm going to tell you about what is probably the single most important feature of tools such as Visual Studio and SharpDevelop. If you skip a line after Console.WriteLine(str), and then type

             str.'ll see a menu come up:

That menu is basically showing you everything you can do with the str variable. This is called Intellisense in Visual Studio. I don't know what it's called in SharpDevelop, but we call it Intellisense anyway. Because when a brand becomes known for a great product, you keep using the name of the brand. Your vacuum cleaner might be Panasonic, but your partner will probably still tell you, "Pass me the hoover" (not convinced? Read "The 22 Immutable Laws of Marketing" by Al Ries and Jack Trout). Intellisense is great because it guides you from within the editor as you try out different things.

[Edit: Andreas Grech points out that the idea of code completion predates Visual Studio and SharpDevelop.  "In SharpDevelop, it's referred to as intelli-sense", he says. "The intelli-sense in Visual Studio is then specifically called IntelliSense."]

The first thing that comes up in Intellisense is the Length. Every String has a length, which is the number of characters in the String. For "Hello!" that would be 6. In our new code, we can add the following to find the length:

            Console.WriteLine("Length: {0}", str.Length);

That gives a length of 44. Length is a property (more about them another time), so we don't need to put brackets such as str.Length(). On the other hand, ToUpper() is a function or method, so it must be done like this:


What this does is convert the String to uppercase (results below). Similarly, ToLower() converts a String to lowercase. Note that this does not affect str itself; a new String is returned in each case.

Another interesting method is Replace(). You can replace single characters, or entire sections of the String, with something else:

            String cat = str.Replace("dog""cat");

This gives you: "The quick brown fox jumps over the lazy cat.". We're replacing "dog" with "cat" within the str variable and placing the result in the cat variable.

That's all for today! We'll use Strings a lot more in the coming articles, so stay tuned for more!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.