首頁  >  文章  >  後端開發  >  C# 字串插值

C# 字串插值

WBOY
WBOY原創
2024-09-03 15:15:51869瀏覽

格式化、操作和連接字串的過程在C#中稱為字串插值,使用表達式和物件可以作為字串插值操作的一部分。字串插值的這一特性是在C# 版本6 中引入的,在引入字串插值之前,C# 中使用+(加號)運算子和String.Format 方法對字串執行連接操作,透過使用字串插值,可以將字串放置在我們想要的任何位置,可以使用條件,並且可以指定字串前後的空格。

文法:

字串插值的語法如下:

{<interpolatedExpression>[,<alignment>][:<formatString>]}

如果結果是由將包含在內插字串中的內插表達式產生的,則可以使用逗號來表示結果表達式的對齊方式,並且它是可選的。如果對齊值為正,則結果表達式會右對齊。如果對齊值為負,則結果表達式會左對齊。

可以透過使用冒號定義 formatString 來格式化給定的表達式。

C# 中字串插值的工作原理

  • 每當需要格式化、操作和連接字串時,我們都會使用字串插值。
  • 格式化、操作和連接字串的過程在 C# 中稱為字串插值,使用表達式和物件可以作為字串插值操作的一部分。
  • 字串插值的功能是在 C# 版本 6 中引入的,在引入字串插值之前,+(加號)運算子和 String. C#中使用Format方法對字串進行串聯操作。
  • 透過使用字串插值,可以將字串放置在我們想要的任何位置,可以使用條件,並且可以指定字串前後的空格。
  • 使用字串插值得到的表達式可以使用逗號對齊。 。如果對齊值為正,則結果表達式會右對齊。如果對齊值為負,則結果表達式會左對齊。

實作字串插值的範例

以下是範例

範例#1

示範字串插值以連接給定兩個字串的程式。

代碼:

using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called within which two string variables are defined to store the two strings
static void Main(string[] args)
{
string string1 = "to C#";
//string interpolation is used to concatenate the first string with the second string and display the resulting string
string string2 = $"Welcome {string1} !";
//the resulting output which is the concatenation of the given two strings is printed on the screen
Console.WriteLine(string2);
}
}
}

輸出:

C# 字串插值

說明:在上面的程式中,定義了一個名為program的命名空間。然後定義一個名為check的類別。然後呼叫main方法,其中定義了兩個字串變數來儲存這兩個字串。然後使用字串插值將第一個字串與第二個字串連接起來並顯示結果字串。

範例#2

C# 程式示範字串插值以連接給定的四個字串:

代碼:

using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called within which four string variables are defined to store the four strings
static void Main(string[] args)
{
string string1 = "to C#";
//string interpolation is used to concatenate the first string, second string, third string and fourth string and display the resulting string
string string2 = "Welcome";
string string3 = "Learning is fun";
string string4 = $"{string2} {string1}. \n" +
$"{string3}. ";
//the resulting output which is the concatenation of the given four strings is printed on the screen
Console.WriteLine(string4);
}
}
}

輸出:

C# 字串插值

說明:在上面的程式中,定義了一個名為program的命名空間。然後定義一個名為check的類別。然後呼叫main方法,其中定義了四個字串變數來儲存四個字串。然後使用字串插值將第一個字串、第二個字串、第三個字串和第四個字串連接起來並顯示結果字串。

範例 #3

C# 程式示範字串插值以連接給定字串以顯示電子郵件 ID:

代碼:

using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called within which four string variables are defined to store the four strings
static void Main(string[] args)
{
string string1 = "shobha";
//string interpolation is used to concatenate the first string, second string, display the resulting string which is an email id
string string2 = "shivakumar";
string string3 = $"{string1}.{string2}@gmail.com";
//the resulting output which is an email id is printed on the screen
Console.WriteLine("The given email id after string interpolation is: {0}",string3);
}
}
}

輸出:

C# 字串插值

說明:在上面的程式中,定義了一個名為program的命名空間。然後定義一個名為check的類別。然後呼叫main方法,其中定義了三個字串變數來儲存這三個字串。然後使用字串插值來連接第一個字串和第二個字串,顯示結果字串,即電子郵件 ID。

結論

在本教程中,我們透過定義、語法以及程式設計範例及其輸出來理解字串插值的概念。

以上是C# 字串插值的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 字串格式()下一篇:C# 字串格式()