Effective C # -working with strings (translation)

zhaozj2021-02-16  52

I saw a small short message suitable for beginners, I translated it, the flying knife bricks only came, ready :).

Effective C # -working with strings author: mahesh chand

1. Nicker

Check if a string is empty, an effective way is to replace the use of NULL or "" comparison using the length attribute of the String class. String str1 = amethodreturnstring () {// do something and return a string} f (str1.length> 0) {// do something}

2. String connection

Whenever a string is modified to return a new string, establishing a number of string logs reduces the performance of your program, you can use the StringBuilder class to avoid building a new string instance. You want to connect two strings, this is the usual method: string str1 = "i like"; string str2 = "soccer"; string strconcat = string.concat (str1, str2);

The result of StrConcat is "I Like Soccer", you can use the StringBuilder class Append method to do the same thing. Stringbuilder MyStrBuilder = New StringBuilder ("i like"); string newstr = "soccer"; MyStrBuilder.Append (newstr);

The result of MyStrBuilder is also I like Soccer.

3. Compare string

Use the string.equals method to compare two strings. String str1 = amethodreturnsString ()

IF (str1.equals ("teststing")) {// do something}

转载请注明原文地址:https://www.9cbs.com/read-22932.html

New Post(0)