Substring function int length end start 1 if length

Info icon This preview shows pages 383–386. Sign up to view the full content.

View Full Document Right Arrow Icon
// substring function int length = (end - start) + 1; if (length == inputString.Length) { // If we didn't trim anything, just return the // input string (don't create a new one return inputString; } // If the length is zero, then return the empty string if (length == 0) { return string.Empty; } return inputString.Substring(start, length); } This method works by iterating through our string, examining each character and checking to see whether it should be trimmed. If so, then we increment the start position by one character, and check the next one, until we hit a character that should not be trimmed, or the end of the string. We then do the same thing starting from the end of the string, and reversing character by character until we reach the start point. Trimming Whitespace | 359
Image of page 383

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

View Full Document Right Arrow Icon
If you wanted to write the equivalent of TrimStart or TrimEnd you would just optionally leave out the end or start checking, respectively. Finally, we create our new output string, by using the Substring method we looked at earlier. Notice how we’ve avoided creating strings unnecessarily; we don’t build up the results as we go along, and we don’t create new strings in the “no change” and “empty” cases. (We could have written a much shorter function if we weren’t worried about this: inputString.Trim().Trim(characters) would have done the whole job! However, with two calls to Trim , we end up generating two new strings instead of one. You’d need to measure your code’s performance in realistic test scenarios to find out whether the more complex code in Example 10-79 is worth the effort. We’re showing it mainly to illustrate how to dig around inside a string.) The interesting new bit of code, though, is that char.IsWhitespace method. Checking Character Types We’re generally familiar with the idea that characters might be numbers, letters, white- space, or punctuation. This is formalized in the .NET Framework, and char provides us with a bunch of static helper functions to do the categorization for us. Several are fairly self-explanatory: IsWhitespace , IsLetter , IsDigit , IsLetterOrDigit , IsPunctuation There are also a couple of useful items for testing whether a character is upper- or lowercase: IsUpper , IsLower Then there are a few less intuitively obvious items: IsNumber (you might wonder whether there was a difference between this and IsDigit ?) IsSeparator , IsControl IsHighSurrogate , IsLowSurrogate Even the self-explanatory items turn out to be a little more complicated than you might think. These categories come from Unicode, and to understand that, we need to delve a little more deeply into the way that characters are encoded. Encoding Characters When we give a char variable the value 'A' , what exactly is that value? 360 | Chapter 10: Strings
Image of page 384
We’ve already alluded to the fact that there is some kind of encoding going on— remember that we mentioned the IBM-derived Latin1 scheme when we were discussing escaped character literals.
Image of page 385

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

View Full Document Right Arrow Icon
Image of page 386
This is the end of the preview. Sign up to access the rest of the document.

{[ 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