【发布时间】:2019-05-07 06:40:06
【问题描述】:
我知道有人问过这个问题。而且我并不是真的在寻找这样做的功能。我希望能得到一些技巧,让我做得更好。基本上,取一个长字符串,然后在其中搜索一个较小的字符串。我知道,实际上总是有一百万种方法可以让事情变得更好,这就是把我带到这里的原因。
请看一下代码 sn-p,让我知道你的想法。不,它不是很复杂,是的,它确实可以满足我的需求,但我更感兴趣的是了解痛点将在哪里使用它,因为我认为它会起作用,但不会出于这样那样的原因。我希望这是有道理的。但是为了给这个问题一个回答的方式,这是执行这项任务的一种强有力的方式吗(我有点知道答案:))
对建设性的批评非常感兴趣,而不仅仅是“那很糟糕”。我恳请您详细说明这样的想法,以便我可以充分利用回复。
public static Boolean FindTextInString(string strTextToSearch, string strTextToLookFor)
{
//put the string to search into lower case
string strTextToSearchLower = strTextToSearch.ToLower();
//put the text to look for to lower case
string strTextToLookForLower = strTextToLookFor.ToLower();
//get the length of both of the strings
int intTextToLookForLength = strTextToLookForLower.Length;
int intTextToSearch = strTextToSearchLower.Length;
//loop through the division amount so we can check each part of the search text
for(int i = 0; i < intTextToSearch; i++)
{
//substring at multiple positions and see if it can be found
if (strTextToSearchLower.Substring(i,intTextToLookForLength) == strTextToLookForLower)
{
//return true if we found a matching string within the search in text
return true;
}
}
//otherwise we will return false
return false;
}
【问题讨论】:
-
请提供一些示例数据和预期结果。
-
我认为代码审查网站正是你要找的
-
查看
Likeoperator。它不仅仅是一个简单的FindTextInString。但是,如果您只是寻找“FindTextInString”,它可以完成这项工作,但使用string.Contains会更好。 -
最接近的匹配是使用正则表达式,它提供相同的功能,但有一些语法差异。
-
或者,
.Split(...).Length > 1(真的不要那样做)
标签: c# string string-matching