我是编程语言的新手。我有一个要求,我必须根据搜索字符串返回记录。在C#中搜索字符串中每个单词的前几个字符
例如,采取以下三个记录与 “卡尔” 的搜索字符串:
加州大学
帕斯卡尔研究所
加州大学
我试过String.Contains
,但所有三个都返回。如果我使用String.StartsWith
,我只得到第3条记录。我的要求是返回结果中的#1和#3。
谢谢你的帮助。
我是编程语言的新手。我有一个要求,我必须根据搜索字符串返回记录。在C#中搜索字符串中每个单词的前几个字符
例如,采取以下三个记录与 “卡尔” 的搜索字符串:
加州大学
帕斯卡尔研究所
加州大学
我试过String.Contains
,但所有三个都返回。如果我使用String.StartsWith
,我只得到第3条记录。我的要求是返回结果中的#1和#3。
谢谢你的帮助。
我喜欢这种为了简单:
if(str.StartsWith("Cal") || str.Contains(" Cal")){
//do something
}
你可以试试:
foreach(var str in stringInQuestion.Split(' '))
{
if(str.StartsWith("Cal"))
{
//do something
}
}
一个很好的答案,只要确保你没有一个字符串能够不止一次地满足这个'if'语句,就像“加州研究所的愈伤组织”:) – davehale23
哦,我的好点 –
如果您使用.NET 3.5或更高版本,我建议你使用LINQextension methods。检查出String.Split
和Enumerable.Any
。喜欢的东西:
string myString = "University of California";
bool included = myString.Split(' ').Any(w => w.StartsWith("Cal"));
Split
划分myString
在空格字符,并返回字符串的数组。 Any
对数组起作用,如果任何字符串以"Cal"
开头,则返回true。
如果你不想或不能使用Any
,那么你将不得不手动循环遍历单词。
string myString = "University of California";
bool included = false;
foreach (string word in myString.Split(' '))
{
if (word.StartsWith("Cal"))
{
included = true;
break;
}
}
只是出于好奇,是否有Lambda表达式的新手?我喜欢这个答案,但我不确定新编程人员会如何遵循它。 –
它可能会混淆他们很多我猜。就我个人而言,我会避免与新手一起使用Lambdas,以防止学习曲线过于陡峭,尽管所提出的解决方案绝对是一个很好的解决方案。 –
我说它永远不会太早开始!但为了以防万一,我还包括了一个for-each示例。 – FishBasketGordo
您可以使用正则表达式查找匹配。下面是一个例子
//array of strings to check
String[] strs = {"University of California", "Pascal Institute", "California University"};
//create the regular expression to look for
Regex regex = new Regex(@"Cal\w*");
//create a list to hold the matches
List<String> myMatches = new List<String>();
//loop through the strings
foreach (String s in strs)
{ //check for a match
if (regex.Match(s).Success)
{ //add to the list
myMatches.Add(s);
}
}
//loop through the list and present the matches one at a time in a message box
foreach (String matchItem in myMatches)
{
MessageBox.Show(matchItem + " was a match");
}
string univOfCal = "University of California";
string pascalInst = "Pascal Institute";
string calUniv = "California University";
string[] arrayofStrings = new string[]
{
univOfCal, pascalInst, calUniv
};
string wordToMatch = "Cal";
foreach (string i in arrayofStrings)
{
if (i.Contains(wordToMatch)){
Console.Write(i + "\n");
}
}
Console.ReadLine();
}
除了代码之外,您还应该提供一些内容,具体请提供一些有关您的代码实际在做什么以及为什么会起作用的信息。 OP已经声明他/她是新编程的,所以一段代码没有评论它的工作原理,对此不会有太大的帮助。 – psubsee2003
var strings = new List<string> { "University of California", "Pascal Institute", "California University" };
var matches = strings.Where(s => s.Split(' ').Any(x => x.StartsWith("Cal")));
foreach (var match in matches)
{
Console.WriteLine(match);
}
输出:
University of California
California University
其实,这是一个很好的用例正则表达式。
string[] words =
{
"University of California",
"Pascal Institute",
"California University"
}
var expr = @"\bcal";
var opts = RegexOptions.IgnoreCase;
var matches = words.Where(x =>
Regex.IsMatch(x, expr, opts)).ToArray();
的 “\ b” 的任何单词边界(标点符号,空格等)相匹配。
使用string.Contains,并放下第二个。如果您正在查找记录中任何单词的开头,则需要对每个记录进行“拆分()”,并对拆分数组中的每个元素执行StartsWith。 –
查看string.split(),然后遍历每个单词。 –