我有一个字符串,我想将前三个字符删除。使用子字符串该怎么做,还是有其他方法?
string temp = "01_Barnsley"
string textIWant = "Barnsley"
谢谢你
我有一个字符串,我想将前三个字符删除。使用子字符串该怎么做,还是有其他方法?
string temp = "01_Barnsley"
string textIWant = "Barnsley"
谢谢你
你可以使用String.Substring(Int32)
方法。
从该实例中检索子字符串。子字符串从指定的字符位置开始,一直到字符串的末尾。
string textIWant = temp.Substring(3);
这里有一个演示
。
另外,您也可以使用String.Remove method(Int32, Int32)
方法。
返回一个新字符串,其中从指定位置开始的当前实例中指定数量的字符已被删除。
string textIWant = temp.Remove(0, 3);
这里是一个演示
。
you can use String.Substring Method (Int32)
string textIWant = temp.Substring(3);
或者String.Remove 方法 (Int32, Int32)
string textIWant = temp.Remove(0,3);
\d
表示数字,使用+
表示1个或多个)后面跟着一个下划线,那就是要排除的模式。现在我们告诉解析器我们想要捕获什么,通过使用( )
符号来表示我们想要一个组匹配。在这个子组中,我们使用.+
来表示捕获所有内容。句点(.
)表示任何字符,前面已经见过的+
表示1个或多个。整个模式的完整匹配(不是我们想要的)被分组为索引0。我们想要的第一个子组匹配在索引1处,也就是我们的数据。Console.WriteLine (Regex.Match("01_Barnsley", @"\d+_(.+)").Groups[1].Value); // Barnsley
Console.WriteLine (Regex.Match("9999_Omegaman", @"\d+_(.+)").Groups[1].Value); // Omegaman
使用LINQ的解决方案:
string temp = "01_Barnsley";
string textIWant = new string(temp.Skip(3).ToArray());