在.NET Framework中,每個字符都是System.Char結構的一個實例。System.Char類型很簡單,提供了兩個公共只讀常量字段:MinValue和MaxValue。
GetNumericValue方法返回字符的數值形式。
 public static void Go() {  Double d;  // 數字3  d = Char.GetNumericValue('/u0033'); // ‘3’ would work too  Console.WriteLine(d.ToString());	   // Displays "3"  // 普通分數 四分之一  d = Char.GetNumericValue('/u00bc');  Console.WriteLine(d.ToString());	   // Displays "0.25"  // 'A'是大寫拉丁字母A  d = Char.GetNumericValue('A');  Console.WriteLine(d.ToString());	  // Displays "-1"}可以使用三種技術來實現各種數值類型和Char實例的相互轉換。下面按照優先順序來列出
Convert類型IConvertible接口system.String類型String類型直接拍繩子Object,所以是引用類型。因此String類型總是存在在堆上,永遠不會跑到線程棧。提示:如果想換行或者回車,應該使用Environment.NewLine方式。
可以使用C#的+操作符直接將幾個字符串連成一個。
最后,C#提供了一種特殊的字符串處理方式,稱為逐字字符串,通常用于指定文件或目錄的路徑,或者與正則表達式配合使用。
String file= "c://Windows//System32//Notepad.exe";//或者String file = @"c:/Windows/System32/NotePad.exe";String對象最重要的一點就是不可變。也就是說,字符串一經創建就不可以更改。意味著,它允許在一個字符串上執行各種操作,而不實際的更改這個字符串。如果需要頻繁的更改字符串,請使用StringBuilder類。
強烈建議使用Equals、Compare等方法,不適用CompareTo、==和!=等操作符。
C#默認不啟用字符串留用
使用StringBuilder類。
StringBuilder sb = new StringBuilder();有多個構造器,下面解釋一些概念
StringBuilder維護的字符串的長度。如果事先知道要在StringBuilder中放入多少個字符,那么構造StringBilder時,應該自己設置容量。字符數組
 >一個有char結構構成的數組,負責維護字符串的字符內容。
public static void Go() {
 // Construct a StringBuilder to do string manipulations.
 StringBuilder sb = new StringBuilder();
// Perform some string manipulations using the StringBuilder.
 sb.AppendFormat("{0} {1}", "Jeffrey", "Richter").Replace(" ", "-");
// Convert the StringBuilder to a String in
 // order to uppercase all the characters.
 String s = sb.ToString().ToUpper();
// Clear the StringBuilder (allocates a new Char array).
 sb.Length = 0;
// Load the uppercase String into the StringBuilder,
 // and do more manipulations.
 sb.Append(s).Insert(8, "Marc-");
// Convert the StringBuilder back to a String.
 s = sb.ToString();
// Display the String to the user.
 Console.WriteLine(s); // "JEFFREY-Marc-RICHTER"
 }
Microsoft在FCL中定義的許多類型都能同時識別幾種格式。例如,DateTime類型支持用d標識短日期,用D標識長日期,用g標識常規,用M標識月日,用S標識排序,用Y標識年月。
所有內建值類型都支持C標識貨幣格式,用D標識十進制格式,用E標識科學記數法,用G標識常規模式,用N標識數字格式,用P標識百分數,X標識十六進制。
能解析字符串的任何類型都提供了公共靜態方法Parse。方法獲取一個String并獲取類型的實例。使用方法很簡單
// 調用失敗,因為解析的字符串包含空格Int32 x= Int32.Pares(" 123",NumberStylesNone,null);字符串作為最常用的對象,使用起來還是沒有什么難度。有一些難度的聽都沒聽說過,比如最后的安全字符串。努力吧。
新聞熱點
疑難解答