在c#中的特殊字符前修剪一个字符串 [英] trim a string before a special character in c#
本文介绍了在c#中的特殊字符前修剪一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好我想在特殊字符前修剪一个字符串..
假设字符串为str =qwertyuiop @ 345 * 7%6&n>> werwer> ; ABCD
i希望获得>之后的字符;并忽略其余部分。
即结果字符串必须为restr =ABCD
Hi guys i want to trim a string before a special character..
lets say the string is str="qwertyuiop@345*7%6&n>>werwer>ABCD"
i want to get the characters after > and ignore the rest.
i.e the resultant string must be restr="ABCD"
推荐答案
您可以使用 String.Split [ ^ ]。
您可以从特殊字符的索引处获取子字符串:
You could grab a substring starting from the index of the special character:
string newstring = str.Substring(str.IndexOf(">"));
或者,如果必须是指定的长度:
Or, if it has to be a specified length:
int length = 1;
string newstring = str.Substring(str.IndexOf(">"), length);
或者用Regex的方式。
Hi,
or do it the Regex way.
string str = "qwertyuiop@345*7%6&n>ABCD";
string pattern = ".*>(?<result>.*)";
Regex regex = new Regex(pattern);
Match match = regex.Match(str);
if (match.Success)
{
string result = match.Groups["RESULT"].Value;
}
祝你好运,
Stephan
Best regards,
Stephan
这篇关于在c#中的特殊字符前修剪一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文