web analytics
Working with Strings in C# Options
codeling
Posted: Wednesday, March 22, 2017 9:51:44 PM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 784
Points: 3288

The string type represents a sequence of zero or more Unicode characters. string is an alias for String in the .NET Framework.

Sponsor
Posted: Wednesday, March 22, 2017 9:51:44 PM
codeling
Posted: Wednesday, March 22, 2017 9:53:50 PM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 784
Points: 3288

Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects, not references. This makes testing for string equality more intuitive. For example:

  
string a = "hello";  
string b = "h";  
// Append to contents of 'b'  
b += "ello";  
Console.WriteLine(a == b);  
Console.WriteLine((object)a == (object)b);  

This displays "True" and then "False" because the content of the strings are equivalent, but a and b do not refer to the same string instance.

codeling
Posted: Wednesday, March 22, 2017 9:57:46 PM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 784
Points: 3288

Strings are immutable--the contents of a string object cannot be changed after the object is created, although the syntax makes it appear as if you can do this. For example, when you write this code, the compiler actually creates a new string object to hold the new sequence of characters, and that new object is assigned to b. The string "h" is then eligible for garbage collection.

  
string b = "h";  
b += "ello";  
codeling
Posted: Wednesday, March 22, 2017 10:00:20 PM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 784
Points: 3288

The System.String type represents a Unicode character string, and System.Char represents a 16-bit Unicode character. Of course, this makes portability and localization to other operating systems—especially systems with large character sets—easy. However, sometimes you might need to interface with external systems using encodings other than UTF-16 Unicode character strings. For times like these, you can employ the System.Text.Encoding class to convert to and from various encodings, including ASCII, UTF-7, UTF-8, and UTF-32. Incidentally, the Unicode format used internally by the runtime is UTF-16.

using System;
using System.Text;

namespace ConvertExample
{
   class ConvertExampleClass
   {
      static void Main()
      {
         string unicodeString = "This string contains the unicode character Pi(\u03a0)";
         // Create two different encodings.
         Encoding ascii = Encoding.ASCII;
         Encoding unicode = Encoding.Unicode;

         // Convert the string into a byte[].
         byte[] unicodeBytes = unicode.GetBytes(unicodeString);
         // Perform the conversion from one encoding to the other.
         byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);

         // Convert the new byte[] into a char[] and then into a string.
         // This is a slightly different approach to converting to illustrate
         // the use of GetCharCount/GetChars.
         char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
         ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
         string asciiString = new string(asciiChars);
         // Display the strings created before and after the conversion.
         Console.WriteLine("Original string: {0}", unicodeString);
         Console.WriteLine("Ascii converted string: {0}", asciiString);
      }
   }
}
Users browsing this topic
Guest

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.


© 2017 Digcode.com. All rights reserved.