C# 截取字符串

在 C# 中,可以使用 Substring 方法来截取字符串的一部分。该方法有两个参数:起始索引和要截取的字符数。

以下是使用 Substring 方法截取字符串的示例:

csharp 复制代码
string str = "Hello World";
string result = str.Substring(6); // 从索引为6的位置开始截取
Console.WriteLine(result); // 输出 "World"

在上述示例中,我们使用 Substring(6) 来截取字符串 str,从索引为6的位置开始截取到字符串的末尾。

如果想要截取字符串的一部分,可以提供起始索引和要截取的字符数,如下所示:

csharp 复制代码
string str = "Hello World";
string result = str.Substring(0, 5); // 从索引为0的位置开始截取,截取5个字符
Console.WriteLine(result); // 输出 "Hello"

在上述示例中,我们使用 Substring(0, 5) 来截取字符串 str,从索引为0的位置开始截取5个字符,即 "Hello"。

相关推荐
小码编匠11 小时前
WPF 中的高级交互通过右键拖动实现图像灵活缩放
后端·c#·.net
唐青枫18 小时前
C#.NET 定时任务与队列利器:Hangfire 完整教程
c#·.net
hez20101 天前
Runtime Async - 步入高性能异步时代
c#·.net·.net core·clr
mudtools2 天前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫2 天前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
侃侃_天下3 天前
最终的信号类
开发语言·c++·算法
christine-rr3 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
東雪蓮☆3 天前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
echoarts3 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust