Concatenating strings string fragment1 to be string

Info icon This preview shows pages 368–371. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 10-55. Concatenating strings string fragment1 = "To be, "; string fragment2 = "or not to be."; string composedString = fragment1 + fragment2 ; Console.WriteLine(composedString); 344 | Chapter 10: Strings
Image of page 368

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

View Full Document Right Arrow Icon
Here, we’ve used the + operator to concatenate two strings. The C# compiler turns this into a call to the String class’s static method Concat , so Example 10-56 shows the equivalent code. Example 10-56. Calling String.Concat explicitly string composedString2 = String.Concat(fragment1, fragment2); Console.WriteLine(composedString2); Don’t forget—we’re taking the first two strings, and then creating a new string that is fragment1.Length + fragment2.Length characters long. The original strings remain unchanged. There are several overloads of Concat , all taking various numbers of strings—this ena- bles you to concatenate multiple strings in a single step without producing intermediate strings. One of the overloads, used in Example 10-57 , can concatenate an entire array of strings. Example 10-57. Concatenating an array of strings static void Main(string[] args) { string[] strings = Soliloquize(); string output = String.Concat(strings); Console.WriteLine(output); Console.ReadKey(); } private static string[] Soliloquize() { return new string[] { "To be, or not to be--that is the question:", "Whether 'tis nobler 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." }; } If we build and run that example, we’ll see some output like this: To be, or not to be--that is the question:Whether 'tis nobler in the mind to suf ferThe slings and arrows of outrageous fortuneOr to take arms against a sea of t roublesAnd by opposing end them. That’s probably not quite what we meant. We’ve been provided with each line of Hamlet’s soliloquy, and we really want the single output string to have breaks after each line. Instead of using String.Concat , we can instead use String.Join to concatenate all of the strings as shown in Example 10-58 . This lets us insert the string of our choice between each string. Composing Strings | 345
Image of page 369
Example 10-58. String.Join static void Main(string[] args) { string[] strings = Soliloquize(); string output = String. Join ( Environment.NewLine , strings); Console.WriteLine(output); Console.ReadKey(); } Here we’re using the Environment.NewLine constant to get the line-break string appro- priate for our platform (rather than explicitly using "\n" or "\r" or "\r\n" ). For historical reasons, not all operating systems use the same sequence of characters to represent the end of a line. Windows (like DOS before it) mimics old-fashioned printers, where you had to send two control characters: a carriage return (ASCII value 13, or \r in a string or char- acter literal) would cause the print head to move back to the beginning of the line, and then a line feed (ASCII 10, or \n ) would advance the paper up by one line. This meant you could send a text file directly to a printer without modification and it would print correctly, but it pro- duced the slightly clumsy situation of requiring two characters to denote the end of a line. Unix conventionally uses just a single line feed to mark
Image of page 370

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

View Full Document Right Arrow Icon
Image of page 371
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