字符串处理与正则表达式的深入解析
1. 字符串基本操作
在字符串处理中,有几个常用的方法可以帮助我们完成各种任务,如判断字符串结尾、查找子字符串、插入子字符串等。
1.1 判断字符串结尾
可以使用 EndsWith
方法来判断一个字符串是否以指定的子字符串结尾。示例代码如下:
string s3 = @"Liberty Associates, Inc.
provides custom .NET development,
on-site Training and Consulting";
Console.WriteLine("Ends with Training?: {0}", s3.EndsWith("Training"));
Console.WriteLine("Ends with Consulting?: {0}", s3.EndsWith("Consulting"));
输出结果为:
Ends with Training?: False
Ends with Consulting?: True
1.2 查找子字符串位置
IndexOf
方法可以用于查找子字符串在原字符串中首次出现的位置。示例代码如下:
Console.WriteLine("\nThe first