Upper and lowercase rules vary considerably among

Info icon This preview shows pages 372–374. Sign up to view the full content.

View Full Document Right Arrow Icon
ake, arms, against, a, sea, of, troubles, and, by, opposing, end, them. Upper- and lowercase rules vary considerably among cultures, and you should be cautious when using ToUpper and ToLower for this purpose. For culture-insensitive scenarios, there are also methods called ToUpper Invariant and ToLowerInvariant whose results are not affected by the current culture. MSDN provides a considerable amount of resources devoted to culture-sensitive string operations. A good starting point can be found here: Manipulating Text The result of the preceding section was nice and neat; but what if our array of strings had come from a user? Users have a tendency to whack the Return key a few times before they write anything at all, and add spurious spaces and tabs to the beginning and end of lines, particularly when copying and pasting between applications. They might also add commas or periods or something like that, again in the interest of tidi- ness. They might spell things incorrectly. There’s no accounting for what users might do. Let’s simulate that with a new function shown in Example 10-62 . Example 10-62. Simulating messy input private static string[] SoliloquizeLikeAUser() { return new string[] { "", null, " ", String.Empty, " To be, or not to be--that is the question: ", "Whether 'tis nobelr in the mind to suffer,", "\tThe slings and arrows of outrageous fortune ,", "", "\tOr to take arms against a sea of troubles, ", "And by opposing end them.", "", "", "", "", ""}; } 348 | Chapter 10: Strings
Image of page 372

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

View Full Document Right Arrow Icon
Notice their extensive use of the Return key, the tendency to put the odd comma at the end of the line, and the occasional whack of the Tab key at the beginning of lines. Sadly, if we use this function and then print the output using String.Concat like we did in Example 10-57 , we end up with output like this: To be, or not to be--that is the question: Whether 'tis nobelr in the mind to suffer, The slings and arrows of outrageous fortune , Or to take arms against a sea of troubles, And by opposing end them. We can write some code to tidy this up. We can build up our output string, concate- nating the various strings, and cleaning it up as we go. This is going to involve iterating through our array of strings, inspecting them, perhaps transforming them, and then appending them to our resultant string. Example 10-63 shows how we could structure this, although it does not yet include any of the actual cleanup code. Example 10-63. Cleaning up input string[] strings = SoliloquizeLikeAUser(); string output = String.Empty ; // This is equivalent to "" foreach (string line in strings) { // Do something to look at the line... // then... output = output + line + Environment.NewLine; } Console.WriteLine(output); This would work just fine; but look at what happens every time we go round the loop. We create a new string and store a reference to it in output , throwing away whatever was in output before. That’s potentially very wasteful of resources, if we do this a lot.
Image of page 373
Image of page 374
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