<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # C# 中的字符串 > 原文: [https://zetcode.com/lang/csharp/strings/](https://zetcode.com/lang/csharp/strings/) 在 C# 教程的這一部分中,我們將更詳細地處理字符串數據。 字符串在計算機語言中非常重要。 這就是為什么我們將整章專門用于 C# 中的字符串的原因。 ## C# 字符串定義 字符串是字符序列。 在 C# 中,字符串是 Unicode 字符序列。 它是一種數據類型,用于存儲一系列數據值(通常為字節),其中元素通常根據字符編碼代表字符。 當字符串從字面上出現在源代碼中時,稱為字符串字面值。 字符串是對象。 有兩個用于處理字符串的基本類: * `System.String` * `System.Text.StringBuilder` `String`是不可變的字符序列。 `StringBuilder`是可變的字符序列。 在 C# 中,`string`是`System.String`的別名。 `string`是語言關鍵字,`System.String`是.NET 類型。 ## C# 初始化字符串 有多種創建字符串的方法,它們都是不可變的和可變的。 我們將展示其中的一些。 `Program.cs` ```cs using System; using System.Text; namespace Initialization { class Program { static void Main(string[] args) { char[] cdb = { 'M', 'y', 'S', 'q', 'l' }; string lang = "C#"; String ide = "NetBeans"; string db = new string(cdb); Console.WriteLine(lang); Console.WriteLine(ide); Console.WriteLine(db); StringBuilder sb1 = new StringBuilder(lang); StringBuilder sb2 = new StringBuilder(); sb2.Append("Fields"); sb2.Append(" of "); sb2.Append("glory"); Console.WriteLine(sb1); Console.WriteLine(sb2); } } } ``` 該示例顯示了創建`System.String`和`System.Text.StringBuilder`對象的幾種方法。 ```cs using System.Text; ``` 該語句可以不加限制地使用`System.Text.StringBuilder`類型。 ```cs string lang = "C#"; String ide = "NetBeans"; ``` 最常見的方法是根據字符串字面值創建字符串對象。 ```cs string db = new string(cdb); ``` 在這里,我們從字符數組創建一個字符串對象。 `string`是`System.String`的別名。 ```cs StringBuilder sb1 = new StringBuilder(lang); ``` 從`String`創建一個`StringBuilder`對象。 ```cs StringBuilder sb2 = new StringBuilder(); sb2.Append("Fields"); sb2.Append(" of "); sb2.Append("glory"); ``` 我們創建一個空的`StringBuilder`對象。 我們將三個字符串附加到對象中。 ```cs $ dotnet run C# NetBeans MySql C# Fields of glory ``` 運行示例可得出此結果。 ## C# 字符串插值 $特殊字符前綴將字符串字面值標識為插值字符串。 插值字符串是可能包含插值表達式的字符串字面值。 字符串格式化與字符串插值相似。 本章后面將介紹它。 `Program.cs` ```cs using System; namespace Interpolation { class Program { static void Main(string[] args) { int age = 23; string name = "Peter"; DateTime now = DateTime.Now; Console.WriteLine($"{name} is {age} years old"); Console.WriteLine($"Hello, {name}! Today is {now.DayOfWeek}, it's {now:HH:mm} now"); } } } ``` 該示例介紹了 C# 字符串插值。 ```cs Console.WriteLine($"{name} is {age} years old"); ``` 內插變量位于{}括號之間。 ```cs Console.WriteLine($"Hello, {name}! Today is {now.DayOfWeek}, it's {now:HH:mm} now"); ``` 插值語法可以接收表達式或格式說明符。 ```cs $ dotnet run Peter is 23 years old Hello, Peter! Today is Friday, it's 14:58 now ``` 這是輸出。 ## C# 常規字符串 常規字符串可以包含解釋的轉義序列,例如換行符或制表符。 常規字符串放在一對雙引號之間。 `Program.cs` ```cs using System; using System.Text; namespace RegularLiterals { class Program { static void Main(string[] args) { string s1 = "deep \t forest"; string s2 = "deep \n forest"; Console.WriteLine(s1); Console.WriteLine(s2); Console.WriteLine("C:\\Users\\Admin\\Documents"); } } } ``` 該示例打印兩個包含`\t`和`\n`轉義序列的字符串。 ```cs Console.WriteLine("C:\\Users\\Admin\\Documents"); ``` 使用路徑時,必須避開陰影。 ```cs $ dotnet run deep forest deep forest C:\Users\Admin\Documents ``` This is the output. ## C# 逐字字符串 逐字字符串不解釋轉義序列。 逐字字符串以`@`字符開頭。 逐字字符串可用于多行字符串。 `Program.cs` ```cs using System; namespace VerbatimLiterals { class Program { static void Main(string[] args) { Console.WriteLine(@"deep \t forest"); Console.WriteLine(@"C:\Users\Admin\Documents"); var text = @" Not marble, nor the gilded monuments Of princes, shall outlive this powerful rhyme; But you shall shine more bright in these contents Than unswept stone, besmeared with sluttish time."; Console.WriteLine(text); } } } ``` 在此代碼示例中,我們使用逐字字符串。 ```cs Console.WriteLine(@"deep \t forest"); ``` `\t`特殊字符不被解釋; 它僅打印到控制臺。 ```cs Console.WriteLine(@"C:\Users\Admin\Documents"); ``` 使用路徑時,逐字字符串很方便; 不必逃避陰影。 ```cs var text = @" Not marble, nor the gilded monuments Of princes, shall outlive this powerful rhyme; But you shall shine more bright in these contents Than unswept stone, besmeared with sluttish time."; ``` 逐字字符串允許我們創建多行字符串。 ```cs $ dotnet run deep \t forest C:\Users\Admin\Documents Not marble, nor the gilded monuments Of princes, shall outlive this powerful rhyme; But you shall shine more bright in these contents Than unswept stone, besmeared with sluttish time. ``` This is the output. ## C# 字符串是對象 字符串是對象。 它們是引用類型。 字符串是`System.String`或`System.Text.StringBuilder`類的實例。 由于它們是對象,因此有多種方法可用于完成各種工作。 `Program.cs` ```cs using System; namespace Objects { class Program { static void Main(string[] args) { string lang = "Java"; string bclass = lang.GetType().Name; Console.WriteLine(bclass); string parclass = lang.GetType().BaseType.Name; Console.WriteLine(parclass); if (lang.Equals(String.Empty)) { Console.WriteLine("The string is empty"); } else { Console.WriteLine("The string is not empty"); } int len = lang.Length; Console.WriteLine("The string has {0} characters", len); } } } ``` 在此程序中,我們演示了字符串是對象。 對象必須具有一個類名,一個父類,并且還必須具有一些我們可以調用的方法或要訪問的屬性。 ```cs string lang = "Java"; ``` 創建`System.String`類型的對象。 ```cs string bclass = lang.GetType().Name; Console.WriteLine(bclass); ``` 我們確定`lang`變量所引用的對象的類名稱。 ```cs string parclass = lang.GetType().BaseType.Name; Console.WriteLine(parclass); ``` 接收到我們對象的父類。 所有對象都有至少一個父對象-`Object`。 ```cs if (lang.Equals(String.Empty)) { Console.WriteLine("The string is empty"); } else { Console.WriteLine("The string is not empty"); } ``` 對象具有各種方法。 使用`Equals()`方法,我們檢查字符串是否為空。 ```cs int len = lang.Length; Console.WriteLine("The string has {0} characters", len); ``` `Length()`方法返回字符串的大小。 ```cs $ dotnet run String Object The string is not empty The string has 4 characters ``` 這是`stringobjects.exe`程序的輸出。 ## C# 可變&不可變字符串 `String`是不可變字符序列,而`StringBuilder`是可變字符序列。 下一個示例將顯示差異。 `Program.cs` ```cs using System; using System.Text; namespace MutableImmutable { class Program { static void Main(string[] args) { string name = "Jane"; string name2 = name.Replace('J', 'K'); string name3 = name2.Replace('n', 't'); Console.WriteLine(name); Console.WriteLine(name3); StringBuilder sb = new StringBuilder("Jane"); Console.WriteLine(sb); sb.Replace('J', 'K', 0, 1); sb.Replace('n', 't', 2, 1); Console.WriteLine(sb); } } } ``` 這兩個對象都有替換字符串中字符的方法。 ```cs string name = "Jane"; string name2 = name.Replace('J', 'K'); string name3 = name2.Replace('n', 't'); ``` 在`String`上調用`Replace()`方法將導致返回新的修改后的字符串。 原始字符串不變。 ```cs sb.Replace('J', 'K', 0, 1); sb.Replace('n', 't', 2, 1); ``` `StringBuilder`的`Replace()`方法將用新字符替換給定索引處的字符。 原始字符串被修改。 ```cs $ dotnet run Jane Kate Jane Kate ``` 這是程序的輸出。 ## C# 連接字符串 可以使用`+`運算符或`Concat()`方法添加不可變的字符串。 它們將形成一個新字符串,該字符串是所有連接字符串的鏈。 可變字符串具有`Append()`方法,該方法可以從任意數量的其他字符串中構建一個字符串。 也可以使用字符串格式設置和插值來連接字符串。 `Program.cs` ```cs using System; using System.Text; namespace Concatenate { class Program { static void Main(string[] args) { Console.WriteLine("Return" + " of " + "the king."); Console.WriteLine(string.Concat(string.Concat("Return", " of "), "the king.")); StringBuilder sb = new StringBuilder(); sb.Append("Return"); sb.Append(" of "); sb.Append("the king."); Console.WriteLine(sb); string s1 = "Return"; string s2 = "of"; string s3 = "the king."; Console.WriteLine("{0} {1} {2}", s1, s2, s3); Console.WriteLine($"{s1} {s2} {s3}"); } } } ``` 該示例通過連接字符串創建五個句子。 ```cs Console.WriteLine("Return" + " of " + "the king."); ``` 通過使用+運算符形成一個新的字符串。 ```cs Console.WriteLine(string.Concat(string.Concat("Return", " of "), "the king.")); ``` `Concat()`方法連接兩個字符串。 該方法是`System.String`類的靜態方法。 ```cs StringBuilder sb = new StringBuilder(); sb.Append("Return"); sb.Append(" of "); sb.Append("the king."); ``` 通過三次調用`Append()`方法來創建`StringBuilder`類型的可變對象。 ```cs Console.WriteLine("{0} {1} {2}", s1, s2, s3); ``` 字符串以字符串格式形成。 ```cs Console.WriteLine($"{s1} {s2} {s3}"); ``` 最后,使用插值語法添加字符串。 ```cs $ dotnet run Return of the king. Return of the king. Return of the king. Return of the king. Return of the king. ``` 這是示例輸出。 ## C# 使用引號 當我們要顯示引號時,例如在直接語音中,必須對內引號進行轉義。 `Program.cs` ```cs using System; namespace Quotes { class Program { static void Main(string[] args) { Console.WriteLine("There are many stars."); Console.WriteLine("He said, \"Which one is your favourite?\""); Console.WriteLine(@" Lao Tzu has said: ""If you do not change direction, you may end up where you are heading."" "); } } } ``` 本示例打印直接語音。 ```cs Console.WriteLine("He said, \"Which one is your favourite?\""); ``` 在常規字符串中,該字符使用`\`進行轉義。 ```cs Console.WriteLine(@" Lao Tzu has said: ""If you do not change direction, you may end up where you are heading."" "); ``` 在逐字字符串中,引號前面帶有另一個引號。 ```cs $ dotnet run There are many stars. He said, "Which one is your favourite?" Lao Tzu has said: "If you do not change direction, you may end up where you are heading." ``` This is the output of the program. ## C# 比較字符串 我們可以使用`==`運算符比較兩個字符串。 `Program.cs` ```cs using System; namespace CompareString { class Program { static void Main(string[] args) { Console.WriteLine("12" == "12"); Console.WriteLine("17" == "9"); Console.WriteLine("aa" == "ab"); } } } ``` 在示例程序中,我們比較字符串。 ```cs $ dotnet run True False False ``` 這是程序的輸出。 `string.Compare()`方法比較兩個指定的字符串,并返回一個整數,該整數指示排序順序中它們的相對位置。 如果返回的值小于零,則第一個字符串小于第二個字符串。 如果返回零,則兩個字符串相等。 最后,如果返回的值大于零,則第一個字符串大于第二個字符串。 `Program.cs` ```cs using System; namespace CompareString2 { class Program { static void Main(string[] args) { string str1 = "ZetCode"; string str2 = "zetcode"; Console.WriteLine(string.Compare(str1, str2, true)); Console.WriteLine(string.Compare(str1, str2, false)); } } } ``` 有一個可選的第三個`ignoreCase`參數。 它確定是否應履行此案。 ```cs Console.WriteLine(string.Compare(str1, str2, true)); ``` 比較兩個字符串并忽略大小寫。 此行將 0 打印到控制臺。 ## C# 字符串元素 字符串是字符序列。 字符是字符串的基本元素。 `Program.cs` ```cs using System; namespace StringElements { class Program { static void Main(string[] args) { char[] crs = { 'Z', 'e', 't', 'C', 'o', 'd', 'e' }; String s = new String(crs); char c1 = s[0]; char c2 = s[(s.Length - 1)]; Console.WriteLine(c1); Console.WriteLine(c2); int i1 = s.IndexOf('e'); int i2 = s.LastIndexOf('e'); Console.WriteLine("The first index of character e is " + i1); Console.WriteLine("The last index of character e is " + i2); Console.WriteLine(s.Contains("t")); Console.WriteLine(s.Contains("f")); char[] elements = s.ToCharArray(); foreach (char el in elements) { Console.WriteLine(el); } } } } ``` 在第一個示例中,我們將使用不可變的字符串。 ```cs char[] crs = {'Z', 'e', 't', 'C', 'o', 'd', 'e' }; String s = new String(crs); ``` 由字符數組構成一個新的不可變字符串。 ```cs char c1 = s[0]; char c2 = s[(s.Length-1)]; ``` 使用數組訪問符號,我們獲得字符串的第一個和最后一個`char`值。 ```cs int i1 = s.IndexOf('e'); int i2 = s.LastIndexOf('e'); ``` 通過上述方法,我們得到了字符`"e"`的第一個和最后一個出現。 ```cs Console.WriteLine(s.Contains("t")); Console.WriteLine(s.Contains("f")); ``` 使用`Contains()`方法,我們檢查字符串是否包含't'字符。 該方法返回一個布爾值。 ```cs char[] elements = s.ToCharArray(); foreach (char el in elements) { Console.WriteLine(el); } ``` `ToCharArray()`方法從字符串創建一個字符數組。 我們遍歷數組并打印每個字符。 ```cs $ dotnet run Z e The first index of character e is 1 The last index of character e is 6 True False Z e t C o d e ``` This is the example output. 在第二個示例中,我們將使用可變字符串的元素。 `Program.cs` ```cs using System; using System.Text; public class StringBuilderElements { static void Main() { StringBuilder sb = new StringBuilder("Misty mountains"); Console.WriteLine(sb); sb.Remove(sb.Length-1, 1); Console.WriteLine(sb); sb.Append('s'); Console.WriteLine(sb); sb.Insert(0, 'T'); sb.Insert(1, 'h'); sb.Insert(2, 'e'); sb.Insert(3, ' '); Console.WriteLine(sb); sb.Replace('M', 'm', 4, 1); Console.WriteLine(sb); } } ``` 形成可變的字符串。 我們通過刪除,附加,插入和替換字符來修改字符串的內容。 ```cs sb.Remove(sb.Length-1, 1); ``` 這行刪除最后一個字符。 ```cs sb.Append('s'); ``` 刪除的字符將附加回字符串。 ```cs sb.Insert(0, 'T'); sb.Insert(1, 'h'); sb.Insert(2, 'e'); sb.Insert(3, ' '); ``` 我們在字符串的開頭插入四個字符。 ```cs sb.Replace('M', 'm', 4, 1); ``` 最后,我們在索引 4 處替換一個字符。 ```cs $ dotnet run Misty mountains Misty mountain Misty mountains The Misty mountains The misty mountains ``` 從輸出中,我們可以看到可變字符串是如何變化的。 ## C# 字符串連接和拆分 `Join()`連接字符串,`Split()`拆分字符串。 `Program.cs` ```cs using System; namespace JoinSplit { class Program { static void Main(string[] args) { var items = new string[] { "C#", "Visual Basic", "Java", "Perl" }; var langs = string.Join(",", items); Console.WriteLine(langs); string[] langs2 = langs.Split(','); foreach (string lang in langs2) { Console.WriteLine(lang); } } } } ``` 在我們的程序中,我們將連接和分割字符串。 ```cs var items = new string[] { "C#", "Visual Basic", "Java", "Perl" }; ``` 這是一個字符串數組。 這些字符串將被連接。 ```cs string langs = string.Join(",", items); ``` 數組中的所有單詞都被加入。 我們從中構建一個字符串。 每兩個字之間會有一個逗號。 ```cs string[] langs2 = langs.Split(','); ``` 作為反向操作,我們分割了`langs`字符串。 `Split()`方法返回由字符分隔的單詞數組。 在我們的情況下,它是一個逗號字符。 ```cs foreach (string lang in langs2) { Console.WriteLine(lang); } ``` 我們遍歷數組并打印其元素。 ```cs $ dotnet run C#,Visual Basic,Java,Perl C# Visual Basic Java Perl ``` 這是示例的輸出。 ## C# 常用方法 接下來,我們介紹了幾個其他的常見字符串方法。 `Program.cs` ```cs using System; namespace CommonMethods { class Program { static void Main(string[] args) { string word = "Determination"; Console.WriteLine(word.Contains("e")); Console.WriteLine(word.IndexOf("e")); Console.WriteLine(word.LastIndexOf("i")); Console.WriteLine(word.ToUpper()); Console.WriteLine(word.ToLower()); } } } ``` 在上面的示例中,我們介紹了五個字符串方法。 ```cs Console.WriteLine(str.Contains("e")); ``` 如果字符串包含特定字符,則`Contains()`方法返回`True`。 ```cs Console.WriteLine(str.IndexOf("e")); ``` `IndexOf()`返回字符串中字母的第一個索引。 ```cs Console.WriteLine(str.LastIndexOf("i")); ``` `LastIndexOf()`方法返回字符串中字母的最后一個索引。 ```cs Console.WriteLine(str.ToUpper()); Console.WriteLine(str.ToLower()); ``` 字符串的字母通過`ToUpper()`方法轉換為大寫,并通過`ToLower()`方法轉換為小寫。 ```cs $ dotnet run True 1 10 DETERMINATION determination ``` 運行程序。 ## C# 字符串復制與克隆 我們將描述兩種方法之間的區別:`Copy()`和`Clone()`。 `Copy()`方法創建一個新字符串實例,該實例的值與指定的字符串相同。 `Clone()`方法返回對正在克隆的字符串的引用。 它不是堆上字符串的獨立副本。 它是同一字符串上的另一個引用。 `Program.cs` ```cs using System; namespace CopyClone { class Program { static void Main(string[] args) { string str = "ZetCode"; string cloned = (string) str.Clone(); string copied = string.Copy(str); Console.WriteLine(str.Equals(cloned)); // prints True Console.WriteLine(str.Equals(copied)); // prints True Console.WriteLine(ReferenceEquals(str, cloned)); // prints True Console.WriteLine(ReferenceEquals(str, copied)); // prints False } } } ``` 我們的示例演示了兩種方法之間的區別。 ```cs string cloned = (string) str.Clone(); string copied = string.Copy(str); ``` 字符串值被克隆并復制。 ```cs Console.WriteLine(str.Equals(cloned)); // prints True Console.WriteLine(str.Equals(copied)); // prints True ``` `Equals()`方法確定兩個字符串對象是否具有相同的值。 所有三個字符串的內容都是相同的。 ```cs Console.WriteLine(ReferenceEquals(str, cloned)); // prints True Console.WriteLine(ReferenceEquals(str, copied)); // prints False ``` `ReferenceEquals()`方法比較兩個引用對象。 因此,將復制的字符串與原始字符串進行比較將返回`false`。 因為它們是兩個不同的對象。 ## C# 格式化字符串 在下面的示例中,我們將格式化字符串。 .NET Framework 具有稱為復合格式的功能。 `Format()`和`WriteLine()`方法支持它。 方法采用對象列表和復合格式字符串作為輸入。 格式字符串由固定字符串和一些格式項組成。 這些格式項是與列表中的對象相對應的索引占位符。 格式項具有以下語法: ```cs {index[,length][:formatString]} ``` 索引組件是必需的。 它是一個從 0 開始的數字,表示對象列表中的一項。 多個項目可以引用對象列表的同一元素。 如果格式項未引用該對象,則將其忽略。 如果我們在對象列表的范圍之外引用,則會拋出運行時異常。 長度部分是可選的。 它是參數的字符串表示形式中的最小字符數。 如果為正,則該參數為右對齊;否則為 0。 如果為負,則為左對齊。 如果指定,則必須用冒號分隔索引和長度。 `formatString`是可選的。 它是一個格式化值的字符串,是一種特定的方式。 它可以用來格式化日期,時間,數字或枚舉。 在這里,我們展示了如何使用格式項的長度分量。 我們將三列數字打印到終端。 左,中和右對齊。 `Program.cs` ```cs using System; namespace Format1 { class Program { static void Main(string[] args) { int oranges = 2; int apples = 4; int bananas = 3; string str1 = "There are {0} oranges, {1} apples and {2} bananas"; string str2 = "There are {1} oranges, {2} bananas and {0} apples"; Console.WriteLine(str1, oranges, apples, bananas); Console.WriteLine(str2, apples, oranges, bananas); } } } ``` 我們向控制臺打印一條簡單的消息。 我們僅使用格式項的索引部分。 ```cs string str1 = "There are {0} oranges, {1} apples and {2} bananas"; ``` `{0}`,`{1}`和`{2}`是格式項。 我們指定索引組件。 其他組件是可選的。 ```cs Console.WriteLine(str1, oranges, apples, bananas); ``` 現在,我們將復合格式放在一起。 我們有字符串和對象列表(橙色,蘋果,香蕉)。 `{0}`格式項目是指橙色。 `WriteLine()`方法將`{0}`格式項替換為`oranges`變量的內容。 ```cs string str2 = "There are {1} oranges, {2} bananas and {0} apples"; ``` 引用對象的格式項的順序很重要。 ```cs $ dotnet run There are 2 oranges, 4 apples and 3 bananas There are 2 oranges, 3 bananas and 4 apples ``` 我們可以看到該程序的結果。 下一個示例將格式化數字數據。 `Program.cs` ```cs using System; namespace Format2 { class Program { static void Main(string[] args) { Console.WriteLine("{0} {1, 12}", "Decimal", "Hexadecimal"); Console.WriteLine("{0:D} {1,8:X}", 502, 546); Console.WriteLine("{0:D} {1,8:X}", 345, 765); Console.WriteLine("{0:D} {1,8:X}", 320, 654); Console.WriteLine("{0:D} {1,8:X}", 120, 834); Console.WriteLine("{0:D} {1,8:X}", 620, 454); } } } ``` 我們以十進制和十六進制格式打印數字。 我們還使用長度分量對齊數字。 ```cs Console.WriteLine("{0:D} {1,8:X}", 502, 546);; ``` `{0:D}`格式項指定,將采用提供的對象列表中的第一項并將其格式化為十進制格式。 `{1,8:X}`格式項目取第二項。 將其格式化為十六進制格式`:X`。 字符串長度為 8 個字符`8` 。 因為數字只有三個字符,所以它會右對齊并用空字符串填充。 ```cs $ dotnet run Decimal Hexadecimal 502 222 345 2FD 320 28E 120 342 620 1C6 ``` 運行示例,我們得到了這個結果。 最后兩個示例將格式化數字和日期數據。 `Program.cs` ```cs using System; namespace Format3 { class Program { static void Main(string[] args) { Console.WriteLine(string.Format("Number: {0:N}", 126)); Console.WriteLine(string.Format("Scientific: {0:E}", 126)); Console.WriteLine(string.Format("Currency: {0:C}", 126)); Console.WriteLine(string.Format("Percent: {0:P}", 126)); Console.WriteLine(string.Format("Hexadecimal: {0:X}", 126)); } } } ``` 該示例演示了數字的標準格式說明符。 數字 126 以五種不同格式打印:普通,科學,貨幣,百分比和十六進制。 ```cs $ dotnet run Number: 126.00 Scientific: 1.260000E+002 Currency: $126.00 Percent: 12,600.00% Hexadecimal: 7E ``` This is the output of the program. 最后,我們將格式化日期和時間數據。 `Program.cs` ```cs using System; namespace Format4 { class Program { static void Main(string[] args) { DateTime today = DateTime.Now; Console.WriteLine(string.Format("Short date: {0:d}", today)); Console.WriteLine(string.Format("Long date: {0:D}", today)); Console.WriteLine(string.Format("Short time: {0:t}", today)); Console.WriteLine(string.Format("Long time: {0:T}", today)); Console.WriteLine(string.Format("Month: {0:M}", today)); Console.WriteLine(string.Format("Year: {0:Y}", today)); } } } ``` 該代碼示例顯示了當前日期和時間的六種不同格式。 ```cs $ dotnet run Short date: 10/24/2019 Long date: Thursday, October 24, 2019 Short time: 1:37 PM Long time: 1:37:38 PM Month: October 24 Year: October 2019 ``` 這是示例的輸出。 C# 教程的這一部分介紹了字符串。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看