惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

AWS News Blog
AWS News Blog
T
Tenable Blog
Project Zero
Project Zero
T
The Exploit Database - CXSecurity.com
L
LINUX DO - 热门话题
T
Threat Research - Cisco Blogs
T
Threatpost
Security Latest
Security Latest
C
Cisco Blogs
L
Lohrmann on Cybersecurity
S
Security @ Cisco Blogs
Google Online Security Blog
Google Online Security Blog
NISL@THU
NISL@THU
AI
AI
V
Vulnerabilities – Threatpost
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Last Watchdog
The Last Watchdog
G
GRAHAM CLULEY
Cloudbric
Cloudbric
H
Hackread – Cybersecurity News, Data Breaches, AI and More
H
Hacker News: Front Page
U
Unit 42
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MyScale Blog
MyScale Blog
O
OpenAI News
Scott Helme
Scott Helme
V2EX - 技术
V2EX - 技术
P
Proofpoint News Feed
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cyberwarzone
Cyberwarzone
博客园 - 【当耐特】
H
Heimdal Security Blog
S
Schneier on Security
阮一峰的网络日志
阮一峰的网络日志
Help Net Security
Help Net Security
D
DataBreaches.Net
Y
Y Combinator Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
TaoSecurity Blog
TaoSecurity Blog
K
Kaspersky official blog
N
News and Events Feed by Topic
WordPress大学
WordPress大学
P
Palo Alto Networks Blog

博客园 - wuty007

TelegramConsole C# 调用WGC 实现桌面屏幕的捕获 完善基于WPF开发的标尺控件(含实例代码) C# 依赖注入 Microsoft.Extensions.DependencyInjection 实现 控制反转(IOC) C# 获取Windows系统的设备名称 记录 Windows系统开启hyper-v ,部分端口被保留,导致端口不能使用而报错的问题 WPF 调用 Win32的SetWindowDisplayAffinity 函数 实现捕获屏幕时,过滤指定的窗口 记录WPF 在清单列表设置了UIACESS为true,没有签名的报错“从服务器返回了一个参照” WPF 的ListBox 去除默认的Item项的 鼠标hover的背景颜色 WPF 调用 ChangeWindowMessageFilterEx 修改指定窗口 (UIPI) 消息筛选器的用户界面特权隔离 记录一下 WPF进程 SendMessage 发送窗口消息进行进程间通信,存在进程权限无法接受消息的问题 记录 使用PsExec启动System权限的WPF 程序 记录 命令行的 findstr 的使用 排查Windows 下的内存使用率过高,但是任务管理器看不到进程 Everything 支持 多实例 运行 指定应用 在 控制面板或者 设置的安装应用 置灰或隐藏卸载按钮 WPF 通过RawInput 获取 系统全局触摸事件 C# 定时任务 Quartz.NET 的使用 记录一下Windows系统下的命令行参数的字符个数限制 WPF 实现支持动态调整高度的文本显示控件
C# 范围运算符
wuty007 · 2026-03-04 · via 博客园 - wuty007

前言

最近是用Claude Opus的智能体帮我写代码,实现业务需求,在检查其写的代码块,发现对于数组的范围获取,多处使用了:..index、index..、^0等范围计算符号。这代码看上去很是简洁。 于是叫AI帮我写了这一篇博客,学习补充一下基础知识。

image

AI博客正文:

C# 中字符串切片语法 string[..index]string[index..] 的核心区别,这两种写法都是 C# 8.0 引入的范围运算符(Range Operator),用于快速截取字符串的一部分,核心差异在于截取的区间方向不同。

一、核心定义与区别

先明确两个基础概念:

  • ^:索引从末尾开始计数(^1 代表最后一个字符,^0 代表字符串末尾的 “空位置”);
  • ..:范围运算符,格式为 [起始索引..结束索引],遵循左闭右开原则(包含起始索引,不包含结束索引)。
语法含义等价的传统写法(Substring)
str[..index] 截取从字符串开头到 index 位置(不包含 index)的子串 str.Substring(0, index)
str[index..] 截取从 index 位置(包含 index)到字符串末尾的子串 str.Substring(index)str.Substring(index, str.Length - index)

二、代码示例(直观理解)

用一个具体字符串演示,结果更清晰:

string str = "HelloWorld"; // 长度 10,索引 0-9
int index = 5;

// 1. str[..index]:从开头到索引5(不包含5)
string part1 = str[..index]; 
Console.WriteLine(part1); // 输出:Hello(索引0-4)

// 2. str[index..]:从索引5到末尾(包含5)
string part2 = str[index..];
Console.WriteLine(part2); // 输出:World(索引5-9)

// 扩展:结合^的反向索引
string part3 = str[..^3]; // 从开头到倒数第3个字符(不包含)
Console.WriteLine(part3); // 输出:HelloWo(索引0-6)

string part4 = str[^3..]; // 从倒数第3个字符到末尾
Console.WriteLine(part4); // 输出:rld(索引7-9)

三、关键注意事项

  1. 索引越界问题:
    • 如果 index 超过字符串长度(比如 str[..15]str.Length=10),会抛出 ArgumentOutOfRangeException
    • 反向索引 ^n 中,n 不能大于字符串长度(比如 str[..^11] 会报错)。
  2. 空字符串 / 边界值:
    • str[..0]str[str.Length..] 都会返回空字符串;
    • str[..str.Length] 等价于原字符串,str[0..] 也等价于原字符串。
  3. 不可变性:
    字符串是不可变的,切片操作会生成新的字符串实例,而非修改原字符串。

四、和传统 Substring 的对比

虽然切片语法和 Substring 功能重叠,但切片更简洁,尤其是结合反向索引时:

// 传统写法:取最后3个字符
string oldWay = str.Substring(str.Length - 3);

// 切片写法:更直观
string newWay = str[^3..];

总结

  1. 核心区别:[..index] 是 “从头取到 index 前”,[index..] 是 “从 index 取到尾”;
  2. 核心原则:切片遵循左闭右开,反向索引 ^ 让末尾截取更简洁;
  3. 使用建议:简单截取优先用切片语法(代码更短),复杂场景(如动态判断)可结合 Substring