Notice that both of these overloads return a new

Info icon This preview shows pages 367–369. Sign up to view the full content.

Notice that both of these overloads return a new string, containing the relevant portion of the original string. The first overload starts with the character at the specified index, and returns the rest of the string (regardless of how long it might be). The second starts at the specified index, and returns as many characters as are requested. A very common requirement is to get the last few characters from a string. Many plat- forms have this as a built-in function, or feature of their strings, but the .NET Frame- work leaves you to do it yourself. To do so depends on us knowing how many characters there are in the string, subtracting the offset from the end, and using that as our starting index, as Example 10-53 shows. Example 10-53. Getting characters from the righthand end of a string static string Right(string s, int length) { int startIndex = s.Length - length; return s.Substring(startIndex); } Notice how we’re using the Length property on the string to determine the total number of characters in the string, and then returning the substring from that offset (to the end). We could then use this method to take the last six characters of our string, as Exam- ple 10-54 does. Example 10-54. Using our Right method string myString = "This is the silliest stuff that ere I heard."; string subString = Right(myString, 6); Console.WriteLine(subString); If you build and run this sample, you’ll see the following output: heard. Getting a Range of Characters | 343
Image of page 367

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Extension Methods for String You will probably build up an armory of useful methods for dealing with strings. It can be helpful to aggregate them together into a set of extension methods. Here’s an example implementing the Right method that we’ve used as an example in this chapter, but modifying it to work as an extension method, and also providing an equivalent to the version of Substring that takes both a start position and a length: public static class StringExtensions { public static string Right(this string s, int length) { int startIndex = s.Length - length; return s.Substring(startIndex); } public static string Right(this string s, int offset, int length) { int startIndex = s.Length - offset; return s.Substring(startIndex, length); } } By implementing them as extension methods, we can now write code like this: string myString = "This is the silliest stuff that ere I heard."; string subString = myString.Right(6); string subString2 = myString.Right(6, 5); Console.WriteLine(subString); Console.WriteLine(subString2); This will produce output like the following: heard. heard Notice that the Length of the string is the total number of characters in the string— much as the length of an array is the total number of entities in the array, not the number of bytes allocated to it (for example). Composing Strings You can create a new string by composing one or more other strings. Example 10-55 shows one way to do this.
Image of page 368
Image of page 369
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern