C# Substring用法

方法1 Substring(int32)               从此实例检索子字符串。 子字符串在指定的字符位置开始并一直到该字符串的末尾。

方法2 Substring(Int32, Int32)  从此实例检索子字符串。 子字符串从指定的字符位置开始且具有指定的长度。

参数一:起始位置(从0开始)

参数二:指定长度

举例:

string s = "hello world";

string ss;

例子1  从指定位置开始到结尾的字符串(0位开始)

int i=1;

ss = s.Substring(i);

ss = "ello world"

例子2  从指定位置开始取固定长度的字符串

int i=1;

ss = s.Substring(i,3);

ss = "ell"

例子3  返回左边的i个字符

int i=5;

ss = s.Substring(0,i)

ss = "hello"

例子4  返回右边的i个字符

int i=5;

ss = s.Substring(s.Length-i,i);

ss = "world"

例子5  返回两个特定字符之间的字符串

int IndexofA = s.IndexOf('e'); //字符串的话总以第一位为指定位置

int IndexofB = s.IndexOf('r');

ss = s.Substring(IndexofA + 1, IndexofB - IndexofA -1);

ss = "llo wo"

posted @   马克温  阅读(785)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示