C#字符串占位符替换

cs 复制代码
using System;
 
namespace myprog
{
    class test
    {
        static void Main(string[] args)
        {
            string str1 = string.Format("{0}今年{1}岁,身高{2}cm,月收入{3}元;", "小李", 23, 177, 5000);
            Console.WriteLine(str1);
            Console.ReadKey();
        }
    }
}

string.Format()第一个双引号中,{n}是占位符,替换为后面对应的第n个数据,第一个双引号中的其他内容直接输出;运行如下;

比使用加号拼接字符串方便一些;在 Console.WriteLine 中也可以使用;

相关推荐
夜莺悠吟1 小时前
关于对 C# 中 ImplicitUsings,GlobalUsings 的讨论
c#·.net
-银雾鸢尾-8 小时前
C#中的拓展方法
开发语言·c#
EIP低代码平台20 小时前
EIP低代码平台-系统参数配置详解|落地三级等保+高灵活私有化部署方案
低代码·c#·权限·工作流·netcore
王莎莎-MinerU21 小时前
MCP 解决的是工具接入,科研 Agent 还缺的是科学证据接口标准化
开发语言·网络·人工智能·深度学习·pdf·c#·php
-银雾鸢尾-21 小时前
C#中Object类内的方法
开发语言·c#
tiankong12131 天前
如何拓展多态下的子类
设计模式·c#
geats人山人海1 天前
c# 第九章 record
开发语言·c#
-银雾鸢尾-1 天前
C#中的抽象类与抽象方法
开发语言·c#
影寂ldy1 天前
C# Task 进阶:WaitAll / WaitAny / WhenAll / WhenAny
开发语言·c#