在文本c#中查找符号
本文关键字:查找 符号 文本 | 更新日期: 2023-09-27 18:05:16
我正试图从文本中检索如下符号之一:"<","=","<=",">",">=","<>"。
示例文本可能看起来像"> 10 dalk tasd ... "
或CCD_ 2。可能有很多空白字符。
我正在尝试做下面这样的事情,但它不起作用:
string sign = new string(textCh.SkipWhile(c => !Char.IsSymbol('>') || !Char.IsSymbol('=') || !Char.IsSymbol('<') || !Char.IsSymbol('¬'))
.TakeWhile(c => Char.IsSymbol('=') || Char.IsSymbol('>')).ToArray());
我怎样才能取回它?
您不希望SkipWhile(criteria OR criteria OR criteria(,因为您要取的字符只能是<,=,或>,并且与而非的字符相对应的条件将为true,并且该字符将被跳过。
您可以将SkipWhile条件更改为&;,或者你可以使用Regex。
var sign = System.Text.RegularExpressions.Regex.Match("<[>=]?|=|>=?|¬=").Value;
要提取第一个这样的符号,我会使用
Regex.Match(myString, "<[>=]?|=|>=?|!=").Value
string example = "avasvasv>asfascvd<hrthtjh";
int firstIndex = example.IndexOfAny(new char[] { '>', '<', '-', '=' });
int lastIndex = example.Substring(firstIndex + 1).IndexOfAny(new char[] { '=', '>'});
string outPutExample = example.Substring(firstIndex + 1).Substring(0, lastIndex); // OutPut -> asfascvd