发布时间:2025-12-11 00:38:24 浏览次数:6
c#中截取字符串主要是借助Substring 这个函数。
string string.Substring(int startIndex,int length);
说明:
如果传入的参数为两个长整参数,第一个参数指子字符串的起始位置,也就是开始截取的位置,第二个参数指截取的长度。
string string.Substring(int startIndex);
说明:
如果传入的参数为一个大于等于0 的长整数,则以这个长整数的位置为起始位置,截取字符串剩余的全部作为子字符串。
代码示例
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceConsoleApplication1{classSubStringTest{staticvoidMain(string[]args){stringoriString="Hello,Kitty!";//原始字符串stringsubString="";//截取后的字符串//输出:HesubString=oriString.Substring(0,2);//从第一个字符开始,截取2个字符Console.WriteLine(subString);//输出:llosubString=oriString.Substring(2,3);//从第一个‘l'开始,截取3个字符Console.WriteLine(subString);//输出:Kitty!subString=oriString.Substring(6);//从第七个字符开始截取,到原始字符串结束Console.WriteLine(subString);}}}看完上述内容,你们对如何使用Substring截取字符串有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注本站行业资讯频道,感谢大家的支持。