Strings
In C#, you can use strings as array of characters, However, more common practice is to use the string keyword to declare a string variable. The string keyword is an alias for the System.String class.
Creating a String Object
You can create string object using one of the following methods:
· By assigning a string literal to a String variable
· By using a String class constructor
· By using the string concatenation operator (+)
· By retrieving a property or calling a method that returns a string
· By calling a formatting method to convert a value or an object to its string representation
The following example demonstrates this:
using System;
namespace StringApplication
{
class Program
{
static void Main(string[] args)
{
//from string literal and string concatenation
string fname, lname;
fname = "Rowan";
lname = "Atkinson";
string fullname = fname + lname;
Console.WriteLine("Full Name: {0}", fullname);
//by using string constructor
char[] letters = { 'H', 'e', 'l', 'l','o' };
string greetings = new string(letters);
Console.WriteLine("Greetings: {0}", greetings);
//methods returning string
string[] sarray = { "Hello", "From", "Tutorials", "Point" };
string message = String.Join(" ", sarray);
Console.WriteLine("Message: {0}", message);
//formatting method to convert a value
DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
string chat = String.Format("Message sent at {0:t} on {0:D}", waiting);
Console.WriteLine("Message: {0}", chat);
}
}
}
When the above code is compiled and executed, it produces the following result:
Full Name: Rowan Atkinson
Greetings: Hello
Message: Hello From Tutorials Point
Message: Message sent at 5:58 PM on Wednesday, October 10, 2012
String Builder Class
A StringBuilder object is not a string but rather an auxiliary object used for manipulating characters. It contains a buffer, typically initialized with a string but usually larger than that string. This buffer can be manipulated in place without creating a new string: You can insert, append, remove, and replace characters. When you're done manipulating the characters, use StringBuilder's ToString method to extract the finished string from it.
Both String and StringBuilder contain Unicode characters of type Char and support an indexer that returns Char. Because the String class is immutable, its indexer is read-only, but the StringBuilder indexer is readable/writeable. Listing 20.38 illustrates how to manipulate characters with the StringBuilder class and then place the characters into a String object.
Listing 20.38: StringBuilder Class Manipulation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StringBuilderClass
{
class Program
{
static void Main(string[] args)
{
String MyName;
StringBuilder MyOtherName = new StringBuilder("Hello");
MyOtherName.Remove(2, 3); // produces "He"
MyOtherName.Insert(2, "lp"); // produces "Help"
MyOtherName.Replace('l', 'a'); // produces "Heap"
MyName = MyOtherName.ToString();
}
}
}
You can use either String or StringBuilder for string operations. Deciding which to use depends on the frequency of the string modification. If you modify the string frequently-in operations like reassigning, appending, removing, and replacing some characters-you should choose the StringBuilder class. However, if your methods do not change the string value much, registering the String class to the .NET Framework's internal string table will save space in memory for duplicate strings. The framework also provides you faster access to string literal values stored in variables. The .NET Framework automatically handles these operations behind the scenes for you.
No comments:
Post a Comment