C # concise tutorial (4)
Disclaimer: I am in translation, add some of my own opinions and some other comparisons, I hope you can make everyone understand more, understand more.
If you have any suggestions, please mail: efoxxx@263.net
By the way, the brother of "finding the C # compiler" in the expert clinic, how can I have not received the C # compiler you promised, urgent, and send me a busy busy, Thanks!
Efficient C # - string programming
◆ Empty string
Detecting a string is empty is an essential programming requirement.
The most effective way to detect if a string is empty is not to use null, nor is it compared to the "" string, but use the "" "LENGTH attribute with the string class.
String str1 = amethodreturnsString ()
{
// do Something and Return A String
}
IF (str1.length> 0)
{
// do something
}
◆ String connection
Once you have modified a string, it will return a new string. Generating too many string objects when programming will reduce the running efficiency of your program. However, you can avoid generating a new string instance - using the StringBuilder class.
Assume that you need to connect two strings. Low is a traditional method -
String str1 = "i like";
String str2 = "soccer";
String strconcat = string.concat (str1, str2);
The result is: strconcat = "i like Soccer".
You can use the StringBuilder class and its Append method to complete the same work:
Stringbuilder MyStrBuilder = New StringBuilder ("I Like");
String newstr = "soccer";
MyStrBuilder.Append (newstr);
Results MyStrBuilder is "I Like Soccer".
◆ String comparison
Use string.equals method to compare if both strings are equal:
String str1 = amethodreturnsString ()
IF (str1.equals ("teststing"))
{
// do something
}