A string is an object of type String whose value is text. Internally, the text is stored as a sequential read-only collection of Char objects. There is no null-terminating character at the end of a C# string; therefore a C# string can contain any number of embedded null characters ('\0').
As mentioned earlier string is just an alias for the class System.String. You can use either of the two to use strings and each provide you with multiple functions to make your life easier. We will look at each of them one by one.
Now this immutable property has a hidden problem too. Because each time you change or concatenate a string object, C# creates a new reference but if there is another reference pointing to the older object, then it still points to the older object.
.NET has the concept of an "intern pool". It's basically just a set of strings, but it makes sure that every time you reference the same string literal, you get a reference to the same string. This is probably language-dependent, but it's certainly true in C# and VB.NET, and I'd be very surprised to see a language it didn't hold for, as IL makes it very easy to do (probably easier than failing to intern literals). As well as literals being automatically interned, you can intern strings manually with the Intern method, and check whether or not there is already an interned string with the same character sequence in the pool using the IsInterned method. This somewhat unintuitively returns a string rather than a boolean - if an equal string is in the pool, a reference to that string is returned. Otherwise, null is returned. Likewise, the Intern method returns a reference to an interned string - either the string you passed in if was already in the pool, or a newly created interned string, or an equal string which was already in the pool.
As mentioned earlier string is just an alias for the class System.String. You can use either of the two to use strings and each provide you with multiple functions to make your life easier. We will look at each of them one by one.
Immutability
String are immutable. It means they cannot be changed after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object. In the following example, when the contents of x1 and x2 are concatenated to form a single string, the two original strings are unmodified. The += operator creates a new string that contains the combined contents. That new object is assigned to the variable x1, and the original object that was assigned to s1 is released for garbage collection because no other variable holds a reference to it.string x1 = "This is a string "; string x2 = "It is a wonderful object"; x1 += x2; System.Console.WriteLine(x1); // Output: This is a string It is a wonderful object
Now this immutable property has a hidden problem too. Because each time you change or concatenate a string object, C# creates a new reference but if there is another reference pointing to the older object, then it still points to the older object.
string x1 = "This is a string "; string x2 = x1; x1="This will not be printed"; System.Console.WriteLine(x2); // Output: This is a string
Interning
.NET has the concept of an "intern pool". It's basically just a set of strings, but it makes sure that every time you reference the same string literal, you get a reference to the same string. This is probably language-dependent, but it's certainly true in C# and VB.NET, and I'd be very surprised to see a language it didn't hold for, as IL makes it very easy to do (probably easier than failing to intern literals). As well as literals being automatically interned, you can intern strings manually with the Intern method, and check whether or not there is already an interned string with the same character sequence in the pool using the IsInterned method. This somewhat unintuitively returns a string rather than a boolean - if an equal string is in the pool, a reference to that string is returned. Otherwise, null is returned. Likewise, the Intern method returns a reference to an interned string - either the string you passed in if was already in the pool, or a newly created interned string, or an equal string which was already in the pool.
Escape Sequence
If you want to embed some characters inside a string we use escape sequence quite similar to C++Escape sequence | Character name | Unicode encoding |
---|---|---|
\' | Single quote | 0x0027 |
\" | Double quote | 0x0022 |
\\ | Backslash | 0x005C |
\0 | Null | 0x0000 |
\a | Alert | 0x0007 |
\b | Backspace | 0x0008 |
\f | Form feed | 0x000C |
\n | New line | 0x000A |
\r | Carriage return | 0x000D |
\t | Horizontal tab | 0x0009 |
\U | Unicode escape sequence for surrogate pairs. | \Unnnnnnnn |
\u | Unicode escape sequence | \u0041 = "A" |
\v | Vertical tab | 0x000B |
\x | Unicode escape sequence similar to "\u" except with variable length. | \x0041 = "A" |
Substring
Substring is a function that creates a new string from within a string by taking a part of it. It has two overloaded formsSubstring(Int32) | Retrieves a substring that starts at a specified character position till the end. |
Substring(Int32, Int32) | Retrieves a substring that starts at a specified character position and has a specified length. |
Lets clarify it a bit further with few examples
string x1="Great India"; Console.WriteLine(x1.Substring(6,5)); //the output will be : India Console.WriteLine(x1.Substring(6,5)); //the output will be : Great India
IndexOf
Reports the index of the first occurrence of one or more characters, or the first occurrence of a string, within this string.
Name | Description |
---|---|
IndexOf(Char) | Reports the index of the first occurrence of the specified Unicode character in this string. |
IndexOf(String) | Reports the index of the first occurrence of the specified string in this instance. |
IndexOf(Char, Int32) | Reports the index of the first occurrence of the specified Unicode character in this string. The search starts at a specified character position. |
IndexOf(String, Int32) | Reports the index of the first occurrence of the specified string in this instance. The search starts at a specified character position. |
IndexOf(String, StringComparison) | Reports the index of the first occurrence of the specified string in the current String object. A parameter specifies the type of search to use for the specified string. |
IndexOf(Char, Int32, Int32) | Reports the index of the first occurrence of the specified character in this instance. The search starts at a specified character position and examines a specified number of character positions. |
IndexOf(String, Int32, Int32) | Reports the index of the first occurrence of the specified string in this instance. The search starts at a specified character position and examines a specified number of character positions. |
IndexOf(String, Int32, StringComparison) | Reports the index of the first occurrence of the specified string in the current String object. Parameters specify the starting search position in the current string and the type of search to use for the specified string. |
IndexOf(String, Int32, Int32, StringComparison) | Reports the index of the first occurrence of the specified string in the current String object. Parameters specify the starting search position in the current string, the number of characters in the current string to search, and the type of search to use for the specified string. |
LastIndexOf
Reports the index position of the last occurrence of a specified Unicode character or String within this instance. Name | Description |
---|---|
String.LastIndexOf (Char) | Reports the index position of the last occurrence of a specified Unicode character within this instance. |
String.LastIndexOf (String) | Reports the index position of the last occurrence of a specified String within this instance. |
String.LastIndexOf (Char, Int32) | Reports the index position of the last occurrence of a specified Unicode character within this instance. The search starts at a specified character position. |
String.LastIndexOf (String, Int32) | Reports the index position of the last occurrence of a specified String within this instance. The search starts at a specified character position. |
String.LastIndexOf (String, StringComparison) | Reports the index of the last occurrence of a specified string within the current String object. A parameter specifies the type of search to use for the specified string. |
String.LastIndexOf (Char, Int32, Int32) | Reports the index position of the last occurrence of the specified Unicode character in a substring within this instance. The search starts at a specified character position and examines a specified number of character positions. |
String.LastIndexOf (String, Int32, Int32) | Reports the index position of the last occurrence of a specified String within this instance. The search starts at a specified character position and examines a specified number of character positions. |
String.LastIndexOf (String, Int32, StringComparison) | Reports the index of the last occurrence of a specified string within the current String object. Parameters specify the starting search position in the current string, and type of search to use for the specified string. |
String.LastIndexOf (String, Int32, Int32, StringComparison) | Reports the index position of the last occurrence of a specified String object within this instance. Parameters specify the starting search position in the current string, the number of characters in the current string to search, and the type of search to use for the specified string. |
Replace
Returns a new string in which all occurrences of a specified character or String in the current string are replaced with another specified character or String.Name | Description |
---|---|
Replace(Char, Char) | Returns a new string in which all occurrences of a specified Unicode character in this instance are replaced with another specified Unicode character. |
Replace(String, String) | Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string. |
x1 = "Great India"; x1=x1.Replace("i","2"); Console.WriteLine(x1); // Output will be : Great Ind2a
Compare Strings
By comparing two strings we find out if one string is greater,less or equal to the other string.Use basic ordinal comparisons when you have to compare or sort the values of two strings without regard to linguistic conventions. A basic ordinal comparison (System.StringComparison.Ordinal) is case-sensitive, which means that the two strings must match character for character: "and" does not equal "And" or "AND". A frequently-used variation is System.StringComparison.OrdinalIgnoreCase, which will match "and", "And", and "AND". StringComparison.OrdinalIgnoreCase is often used to compare file names, path names, network paths, and any other string whose value does not change based on the locale of the user's computer.
string root = "C:\users"; string root2 = "C:\Users"; // Use the overload of the Equals method that specifies a StringComparison. // Ordinal is the fastest way to compare two strings. bool result = root.Equals(root2, StringComparison.Ordinal); Console.WriteLine("Ordinal comparison: {0} and {1} are {2}", root, root2, result ? "equal." : "not equal."); // To ignore case means "user" equals "User". This is the same as using // String.ToUpperInvariant on each string and then performing an ordinal comparison. result = root.Equals(root2, StringComparison.OrdinalIgnoreCase); Console.WriteLine("Ordinal ignore case: {0} and {1} are {2}", root, root2, result ? "equal." : "not equal."); // A static method is also available. bool areEqual = String.Equals(root, root2, StringComparison.Ordinal);
StringBuilder Class
Represents a mutable string of characters. This class cannot be inherited.String operations in .NET are highly optimized and in most cases do not significantly impact performance. However, in some scenarios such as tight loops that are executing many hundreds or thousands of times, string operations can affect performance. The StringBuilder class creates a string buffer that offers better performance if your program performs many string manipulations. The StringBuilder string also enables you to reassign individual characters, something the built-in string data type does not support.
This class represents a string-like object whose value is a mutable sequence of characters. The value is said to be mutable because it can be modified once it has been created by appending, removing, replacing, or inserting characters.
System.Text.StringBuilder sb = new System.Text.StringBuilder("My Name Is Anthony"); sb[0] = 'Y'; System.Console.WriteLine(sb.ToString()); System.Console.ReadLine(); //Output is Yy Name Is Anthony
StartsWith
Determines whether the beginning of an instance of String matches a specified string.
Name | Description |
---|---|
String.StartsWith (String) | Determines whether the beginning of this instance matches the specified string. |
String.StartsWith (String, StringComparison) | Determines whether the beginning of this string matches the specified string when compared using the specified comparison option. |
String.StartsWith (String, Boolean, CultureInfo) | Determines whether the beginning of this string matches the specified string when compared using the specified culture. |
EndsWith
Determines whether the end of this string instance matches a specified string. Name | Description |
---|---|
String.EndsWith (String) | Determines whether the end of this instance matches the specified string. |
String.EndsWith (String, StringComparison) | Determines whether the end of this string matches the specified string when compared using the specified comparison option. |
String.EndsWith (String, Boolean, CultureInfo) | Determines whether the end of this string matches the specified string when compared using the specified culture. |
Trimming and Removing Characters
You can use one of the trim methods in the System.String class to remove any number of spaces or other characters from a specified position in the string. Method name | Use |
---|---|
String.Trim | Removes white spaces from the beginning and end of a string. |
end(v=String.TrimEnd | Removes characters specified in an array of characters from the end of a string. |
String.TrimStart | Removes characters specified in an array of characters from the beginning of a string. |
String.Remove | Removes a specified number of characters from a specified index position in a string. |
No comments:
Post a Comment