11.07.2015 Views

Beginning C# 2008-from Novice-to-professional - A2Z Dotnet

Beginning C# 2008-from Novice-to-professional - A2Z Dotnet

Beginning C# 2008-from Novice-to-professional - A2Z Dotnet

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 3 ■ LEARNING ABOUT STRING MANIPULATIONS 67Looking at the solution, you see that there are elements <strong>from</strong> the first solution, which wasdiscounted because it did not work properly. This is an example of how quickly solving a bug,seeing that code fail, and discounting the solution can be incorrect. You need <strong>to</strong> think throughwhy something fails, and not just fix the bug <strong>to</strong> get rid of it.■Note All of the solutions involved using string type methods. The string type is very sophisticated andsupports many operations that are commonly performed on text.At this point, we are finished with the translation of the greeting. Next, I’ll point out a coupleof additional items that you need <strong>to</strong> keep in mind when dealing with strings.Quoting StringsYou might have noticed the use of a double quotation marks and single quotation marks(quotes, for short) when the method CompareTo() was called. Which type of quote you usemakes a very big difference. If you use a double quote, as in the following example, then youare defining a string type:"using double quotes"'a'If you use a single quote, as in the following example, you are defining a character:The single quote can be used only with a single character. Think of a single character asa letter, but don’t become <strong>to</strong>o attached <strong>to</strong> that definition, since not all languages have letters. Ifyou attempt <strong>to</strong> use the single quote <strong>to</strong> define a buffer of multiple characters, the <strong>C#</strong> compilerwill generate an error, more commonly called a .NET exception.Character MappingA single character takes 16 bits of space, and the space that a string consumes depends on thenumber of characters in a buffer. If a buffer is 10 characters long, then the entire buffer takesup 160 bits of space. A string type is a reference type, not a value type.A single character is 16 bits long so that a buffer can s<strong>to</strong>re text in a multitude of differentformats. The standardized length is due <strong>to</strong> a standard called Unicode.Consider the character a. Philosophically, how do you know that an a is an a? For humans,that’s easy because our brains are trained <strong>to</strong> recognize the curve and look of an a. Now look atthe Russian letter shown in Figure 3-10.Figure 3-10. A Russian letter

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!