System.String class in C#
System.String is a class specifically designed to store a string and allow a large number of operations on the string. In addition, due to the importance of this data type, C# has its own keyword and associated
syntax to make it particularly easy to manipulate strings using this class.
You can concatenate strings using operator overloads:
string message1 = "Hello"; // returns "Hello"
message1 += ", There"; // returns "Hello, There"
string message2 = message1 + "!"; // returns "Hello, There!"
C# also allows extraction of a particular character using an indexer-like syntax: string message = "Hello";
char char4 = message[4]; // returns 'o'. Note the string is zero-indexed
This enables you to perform such common tasks as replacing characters, removing whitespace, and capitalization. System.String class has many standard methods like Compare, CompareTo, CopyTo, Format, Join, Equals and so on. For example, method CopyTo Copies a specific number of characters from the selected index to an entirely new
instance of an array. public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count) Method Compare(string, string) Compares two specified String objects and returns an integer that indicates their relative position in the sort order. using System;public class Example{ public static void Main() { string s1 = "ani\u00ADmal"; string s2 = "animal"; Console.WriteLine("Comparison of '{0}' and '{1}': {2}", s1, s2, String.Compare(s1, s2)); }} 51. System.Text.StringBuilder class in C#
The processing you can do on a StringBuilder is limited to substitutions and appending or removing text from strings. However, it works in
a much more efficient way. When you construct a string using the String class, just enough memory is allocated to hold the string. The
StringBuilder, however, normally allocates more memory than is actually needed. You, as a developer, have the option to indicate how much memory the StringBuilder should allocate, but if you do not, the
amount will default to some value that depends on the size of the string that the StringBuilder instance is initialized with. The StringBuilder class has two main properties:
➤Length, which indicates the length of the string that it actually contains
➤Capacity, which indicates the maximum length of the string in the memory allocation
Any modifications to the string take place within the block of memory assigned to the StringBuilder instance, which makes appending substrings and replacing individual characters within strings very efficient. Removing or inserting substrings is inevitably still inefficient because it means that the following part of the string has to be moved. Only if you perform some operation that exceeds the capacity of the string is it necessary to allocate new memory and possibly move the entire contained string. In adding extra capacity, based on our experiments the StringBuilder appears to double its capacity if it detects the capacity has been exceeded and no new value for the capacity has been set.
System.Text.StringBuilder class has the following methods: Append(), AppendFormat(), Insert(), Remove(), Replace(), ToString().
Here is the simple example of this class:
Using System;
Using System.Text;
{class Program
{static void Main (string[] args){
StringBuilder hello=new Stringbuilder (“hello I am student”,120);
Hello.AppendFormat(“and IS is my specialty”);}}}
52. Format strings in C#
Format strings replaces each format item in a specified string with the text equivalent of a corresponding object's value. As you probably know, you need to specify the format in which you want a variable displayed when you call Console.WriteLine(). For example, if you want to display the value of a variable in a list box or text box, you will normally use the String.Format() method to obtain the appropriate string representation of the variable. However, the actual format specifiers you use to request a particular format are identical to those passed to Console.WriteLine(). In this example in Console.WriteLine() used format string:
double d = 13.45;
int i = 45;
Console.WriteLine("The double is {0,10:E} and the int contains {1}", d, i);
The format string itself consists mostly of the text to be displayed, but wherever there is a variable to be formatted, its index in the parameter list appears in braces. You might also include other information inside
the braces concerning the format of that item. For example, you can include:
-The number of characters to be occupied by the representation of the item, prefixed by a comma. A negative number indicates that the item should be left-justified, whereas a positive number indicates that it should be right-justified. If the item actually occupies more characters than have been requested, it will still appear in full.
- A format specifier, preceded by a colon. This indicates how you want the item to be formatted. For example, you can indicate whether you want a number to be formatted as a currency or displayed in
scientific notation.
Дата добавления: 2015-09-18 | Просмотры: 726 | Нарушение авторских прав
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
|