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 中也可以使用;

相关推荐
宝桥南山29 分钟前
GitHub Models - 尝试一下使用GitHub Models
microsoft·ai·微软·c#·github·.netcore
hixiong1233 小时前
C# OpenvinoSharp部署INSID3
开发语言·人工智能·ai·c#·openvinosharp
星辰徐哥4 小时前
Unity C#入门:变量的定义与访问权限(public/private)
unity·c#·lucene
leoufung4 小时前
LeetCode 30:Substring with Concatenation of All Words 题解(含 C 语言 uthash 实现)
c语言·leetcode·c#
hacker7074 小时前
Visual Studio安装教程(C#开发版)
ide·c#·visual studio
SKY -dada5 小时前
Understand 使用教程
开发语言·c#·流程图·软件构建·敏捷流程·代码复审·源代码管理
William_cl7 小时前
【C#/.NET 进阶】ASP.NET 架构与最佳实践:DI 依赖注入(IoC 核心)从入门到避坑
c#·asp.net·.net
武藤一雄7 小时前
WPF:MessageBox系统消息框
前端·microsoft·c#·.net·wpf
武藤一雄7 小时前
WPF进阶:万字详解WPF如何性能优化
windows·性能优化·c#·.net·wpf·.netcore·鲁棒性
xiaogutou11211 天前
2026年历史课件PPT模板选购指南:教师备课效率与精度的平衡方案
开发语言·c#