在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
The .NET string class is quite comprehensive, yet some common string functions are missing or not entirely obvious. This article provides quick tips on using .NET strings.
Fill a String with Repeating CharactersTo fill a string with repeating characters, use the string class constructor. For example, to fill a string with twenty asterisks: string s = new string( '*', 20 ); Check for Blank StringA blank string can be represented by a null reference or empty string (String.Empty or ""). If you attempt to call a method on a null string, an exception will occur. Hence, to check for a blank string, you should use the new .NET v2.0 static function String.IsNullOrEmpty:
String.Empty vs. ""? It Doesn't MatterThere has been endless debate on the Web whether it's better to represent an empty string with String.Empty or blank quotes "". However, tests show there is minimal performance difference between String.Empty and "" even when creating a billion empty strings. Reverse a StringThere has been extensive analysis on algorithms to reverse a string. The following is a good balance between speed and clarity and works well with Unicode and alternate character sets: static public string Reverse( string s ) { char[] charArray = s.ToCharArray(); Array.Reverse( charArray ); return new string( charArray ); } Compare StringsBecause a string reference can be null, you should avoid using the equality symbol == or the Compare member function when comparing strings. Instead, use the static String.Compare method. This method has the advantage that it can handle null string references, compare strings ignoring case, and compare strings using a specific culture: if (String.Compare( s1, s2, true ) == 0) Convert String to Numeric ValueEach numeric data type such as int, Int32, double, etc. has a static TryParse method that converts a string to that data type without throwing an exception. The method returns a bool whether the string contained a value with the specified data type. For example: string s = "42"; int i; int.TryParse( s, out i ); Use Literal Strings for File PathsA literal string enables you to use special characters such as a backslash or double-quotes without having to use special codes or escape characters. This makes literal strings ideal for file paths that naturally contain many backslashes. To create a literal string, add the at-sign @ before the string's opening quote. For example: string path = @"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe"; String RightNoticeably absent from the string class is the Right method. But you can replicate it easily using the Substring method. Here is a simple method that wraps this up nicely: static string Right( string s, int count ) { string newString = String.Empty; if (s != null && count > 0) { int startIndex = s.Length - count; if (startIndex > 0) newString = s.Substring( startIndex, count ); else newString = s; } return newString; } IndexOf Ignoring CaseThe string's IndexOf methods are all case-sensitive. Fortunately, the Globalization namespace contains the CompareInfo class that includes a case-insensitive IndexOf method. For example:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论