Example of string in C
- 2020-08-22 22:39:03
- OfStack
When designing C#, string is the most frequently used method. However, sometimes it is easy to make mistakes due to carelessness or weak foundation. Today, this article will summarize string's usage in C# in detail. The details are as follows:
1.string is a reference type. We usually compare string objects by comparing the value of the object rather than the object itself
As shown in the following code:
string strA="abcde";
string strB="abc";
string strC="de";
Console.WriteLine(strA == (strB+strC));//true
Console.WriteLine((object)strA == (object)(strB+strC));//false
Because the string content is the same but it doesn't refer to the same instance
2.string objects are not modifiable
The following code is shown:
string strA="abcde";
strA="aaaaa";
On the surface, the contents of strA are modified. In fact, "abcde" is not modified. Instead, it creates a new object "aaaaa", assigns a reference to that object to strA, and "abcde" is collected as garbage.
3. string creation
Direct assignment:
string strA="abcde";// create 1 A content for abcde the string Object, and then assigns a reference to that object strA
Structure:
char[] arr={'a','b','c','d','e'};
string strA=new string(arr);// Just to list 1 Kind of
Note: No String str=new String("abcde"); string is an alias for String in NET Framework.
4.string parameter passing
string is a reference type and we are trying to change this value in 1 function
The test code is as follows:
static void Main(string[] args)
{
string strA = "abcde";
Deal(strA);
Console.WriteLine(strA);
Console.ReadLine();
}
static void Deal(string str)
{
str = str.Substring(0, 2);
}
Run result: abcde
The reason is that when you pass a parameter of a reference type by value, it is possible to change the data that the reference points to, such as the value of a member of a class. However, you cannot change the value of the reference itself, which can be resolved by passing parameters through the ref keyword.
The revised code is as follows:
static void Main(string[] args)
{
string strA = "abcde";
Deal(strA);
Console.WriteLine(ref strA);
Console.ReadLine();
}
static void Deal(ref string str)
{
str = str.Substring(0, 2);
}
Results: ab
The reference itself, not the copy, is passed
5.null string and empty string
null string: No memory allocated; Empty strings allocate memory, but there is no data in memory.
The test code is as follows:
static void Main(string[] args)
{
string strA = "1";
string strB = string.Empty;
string strC = null;
Console.WriteLine(int.Parse(strA));// correct
Console.WriteLine(int.Parse(strB));// The input string is not formatted correctly
Console.WriteLine(strC.ToString());// An object reference is not set to an instance of the object.
Console.ReadLine();
}
Whether the built-in method string is null or null:
IsNullOrEmpty = if (str == null || str. Equals(String. Empty))
IsNullOrWhiteSpace is equal to if (str == null || str. Equals(String. Empty) || str. Trim().Equals (String. Empty))
6.StringBuilder
See the following test code:
string strA="abc"
for(int i=0;i<10000;i++)
{
strA+="abc";
}
Consolse.WriteLine(strA);
Although the code appears to use string concatenation to append new characters to an existing string named strA, it actually creates a new String object for each concatenation operation. Significantly reduced performance. You can use the StringBuilder class instead of the String class to change the string value multiple times. The StringBuilder object is mutable and when you append or delete a substring in a string, no new object is created, but the original object is modified. After you have modified the value of the StringBuilder object, you can call its ES122en.ToString method to convert it to a string
The modified test code is as follows:
StringBuilder strA=new StringBuilder();
for(int i=0;i<10000;i++)
{
strA.Append("abc");
}
Consolse.WriteLine(strA.ToString());
It is believed that the examples described in this paper will be helpful and useful for you to have a firm grasp of the usage of C# string.