C# String Tips (C#String 类型的相关用法 收集)
2010-04-18 14:17
489 查看
C# String Tips
Posted by timm 52 Comments »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 Characters
To 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 String
A 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.IsNullOrEmpty( s )
String.Empty vs. ""? It Doesn't Matter
There 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 String
There 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 Strings
Because 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 Value
Each 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 Paths
A 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 Right
Noticeably 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 Case
The 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:相关文章推荐
- C#中键值对类型Hashtable与Dictionary比较和相关用法
- C#中键值对类型Hashtable与Dictionary比较和相关用法
- 31、C#:键值对类型Hashtable与Dictionary比较和相关用法
- (转)C#中键值对类型Hashtable与Dictionary比较和相关用法
- C#学习日记15----引用类型 之 string类型用法总结
- C# 中WebService返回类型(string,int,bool,DataSet,class实体类)示例
- C#中string转成int类型
- C#中StringBuilder用法以及和String的区别
- 理解C#中的string类型
- java FinalClass String 相关的用法
- C# tips ---关于可空类型
- C#中string类型与int类型的转换
- C#语言之“string格式的日期时间字符串转为DateTime类型”的方法
- C# String.Format用法和格式说明
- String是值类型还是引用类型(C#)
- 标准C++中string类型的用法总结
- "int i=1" "int i=new int() "和“String str = "a";” “String str = new String("a")”区别以及c#值类型和引用类型
- c#中把String转换为Eunm类型
- C#中string类型赋值问题
- 关于c#里的string 类型(转http://xianyun0216.blog.51cto.com/714887/167194)