C# 字符串插值

WBOY
发布: 2024-09-03 15:15:51
原创
868 人浏览过

格式化、操作和连接字符串的过程在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中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!