Modifying the decimal separator double value 18

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

View Full Document Right Arrow Icon
Example 10-48. Modifying the decimal separator double value = 1.8; NumberFormatInfo nfi = new NumberFormatInfo(); nfi. NumberDecimalSeparator = "^"; Console.WriteLine(value.ToString(nfi)); Here we use the NumberFormatInfo to change the decimal separator to the circumflex (hat) symbol. The resultant output is: 1^8 You can use this to control all sorts of features of the formatting engine, such as the default precision, percentage and positive/negative symbols, and separators. Now that we know how to format strings of various kinds, we’ll go back to looking at some of the features of the string itself. In particular, we’ll look at how to slice and dice an existing string in various ways. 340 | Chapter 10: Strings
Image of page 364

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

View Full Document Right Arrow Icon
Accessing Characters by Index Earlier, we saw how to enumerate the characters in a string; however, we often want to be able to retrieve a character at a particular offset into the string. String defines an indexer , so we can do just that. Example 10-49 uses the indexer to retrieve the character at a particular (zero-based) index in the string. Example 10-49. Retrieving characters with a string’s indexer string myString = "Indexing"; char theThirdCharacter = myString[2]; Console.WriteLine(theThirdCharacter); If you execute that code in a console application, you’ll see: d What if we try to use the indexer to assign a value (i.e., to replace the character at that location in the string) as in Example 10-50 ? Example 10-50. Trying to assign a value with a string’s indexer string myString = "Indexing"; myString[2] = 'f'; // Will fail to compile Well, that doesn’t compile. We get an error: Property or indexer 'string.this[int]' cannot be assigned to -- it is read only So, the indexer is read-only. This is a part of a very important constraint on a String object. Strings Are Immutable Once a string has been created, it is immutable . You can’t slice it up into substrings, trim characters off it, add characters to it, or replace one character or substring with another. “What?” I hear you ask. “Then how are we supposed to do our string processing?” Don’t worry, you can still do all of those things, but they don’t affect the original string—copies (of the relevant pieces) are made instead. Why did the designers of the .NET Framework make strings immutable? All that copy- ing is surely going to be an overhead. Well, yes, it is, and sometimes you need to be aware of it. That being said, there are balancing performance improvements when dealing with unchanging strings. The framework can store a single instance of a string and then any variables that reference that particular sequence of characters can reference the same instance. This can actually save on allocations and reduce your working set. And in multithreaded scenarios, the fact that strings never change means it’s safe to use them Strings Are Immutable | 341
Image of page 365
without the cross-thread coordination that is required when accessing modifiable data.
Image of page 366

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

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