搜索单词如果找到返回文本,则返回布尔值false [英] searching words if found return text else return a boolean false

查看:90
本文介绍了搜索单词如果找到返回文本,则返回布尔值false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void GetAgreementScope(string contents, System.IO.FileInfo file)
        {
            var regex = new Regex(@".*shall inure to all Bank Affiliates.*");
            var texts = contents.Split('.');
            foreach (var t in texts)
            {
                if (regex.IsMatch(t))
                {
                    var AScope = (t.Trim());
                    this.AgreementScope = Regex.Replace(AScope, @"\d", "");
                }
            }
        }







i希望代码是类似



当发现返回文本时,从文本文件中搜索应该对所有银行关联公司或者返回新字段bool = false。



例如



搜索适用于所有银行关联公司。

如果找到则AgreementScope =整个包含这组词的文字

else

rights = false;布尔值







请帮助...




i want code to be something like

search "shall inure to all Bank Affiliates" from a text file when found return text else return a new field bool = false .

For eg

search shall inure to all Bank Affiliates.
if found AgreementScope = the whole text containg this set of words
else
rights = false ; boolean value



pls pls help ...

推荐答案

没有人指出你的想法是一些错误的列表:



1)你不能有两个不同的回报单一方法的类型。你要么返回一个字符串或一个bool,而不是两者。相反,如果找不到文本,则可以返回null或空字符串。



2)如果找到了搜索字符串,则可以返回找到的文本in,但是什么决定了你要返回的文本边界?它是整个文件的内容还是什么?



3)你把你的方法称为Get something ,但是你没有从它。这不是我所期望的那种方法的行为。 get方法应该总是返回一些东西。
What nobody has pointed out is a list of a few things wrong with your idea:

1) You can't have two different return types from a single method. Either you're returning a string or a bool, not both. Instead, you can return a null or empty string if the text is not found.

2) If your search string is found, you can return the text it was found in, but what determines the "text" boundaries you're going to return?? Is it the whole file contents or what?

3) You called your method a Getsomething, but you don't return anything from it. That's not what I would expect the behavior of that method to be. "Get" method should always return something.


为什么不使用string.Contains?

Why not just use string.Contains?
if (t.Contains("shall inure to all Bank Affiliates."))
   {
   ...
   }

或者我错过了什么?





[edit]

试试这个:

Or am I missing something?


[edit]
Try this:

private string GetAgreementScope(string contents, System.IO.FileInfo file)
        {
            var texts = contents.Split('.');
            foreach (var t in texts)
            {
                if (t.Contains("shall inure to all Bank Affiliates."))
                {
                    return t;
                }
            }
        return null;
        }

[/ edit]


如果你想从文件中找到文字



If you want find text from file

bool IsTextMatch(string text)
   {
       List<string> lines = new List<string>(File.ReadAllLines("file"));
       //Option 1
       //int lineIndex = lines.FindIndex(line => line.StartsWith(text));
       //Option 2
       int lineIndex = lines.FindIndex(line => line.Contains(text));
       if (lineIndex != -1)
       {
           return true;//Yes it matched
       }
       else return false;
   }


这篇关于搜索单词如果找到返回文本,则返回布尔值false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆