C# 如何检查单词是否以给定字符开头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15527051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a word starts with a given character?
提问by sara
I have a list of a Sharepoint items: each item has a title, a description and a type.
I successfully retrieved it, I called it result
. I want to first check if there is any item in result
which starts with A then B then C, etc. I will have to do the same for each alphabet character and then if I find a word starting with this character I will have to display the character in bold.
我有一个 Sharepoint 项目的列表:每个项目都有一个标题、一个描述和一个类型。我成功找回了它,我称之为result
。我想首先检查是否有任何项目result
以 A 开头,然后是 B 然后是 C,等等。我必须对每个字母字符执行相同的操作,然后如果我找到以该字符开头的单词,我将不得不显示字符加粗。
I initially display the characters using this function:
我最初使用这个函数显示字符:
private string generateHeaderScripts(char currentChar)
{
string headerScriptHtml = "$(document).ready(function() {" +
"$(\"#myTable" + currentChar.ToString() + "\") " +
".tablesorter({widthFixed: true, widgets: ['zebra']})" +
".tablesorterPager({container: $(\"#pager" + currentChar.ToString() +"\")}); " +
"});";
return headerScriptHtml;
}
How can I check if a word starts with a given character?
如何检查单词是否以给定字符开头?
采纳答案by Dmitriy Khaykin
To check one value, use:
要检查一个值,请使用:
string word = "Aword";
if (word.StartsWith("A"))
{
// do something
}
You can make a little extension method to pass a list with A, B, and C
你可以做一个小扩展方法来传递一个带有 A、B 和 C 的列表
public static bool StartsWithAny(this string source, IEnumerable<string> strings)
{
foreach (var valueToCheck in strings)
{
if (source.StartsWith(valueToCheck))
{
return true;
}
}
return false;
}
if (word.StartsWithAny(new List<string>() { "A", "B", "C" }))
{
// do something
}
AND as a bonus, if you want to know what your string starts with, from a list, and do something based on that value:
并且作为奖励,如果您想从列表中知道字符串以什么开头,并根据该值执行某些操作:
public static bool StartsWithAny(this string source, IEnumerable<string> strings, out string startsWithValue)
{
startsWithValue = null;
foreach (var valueToCheck in strings)
{
if (source.StartsWith(valueToCheck))
{
startsWithValue = valueToCheck;
return true;
}
}
return false;
}
Usage:
用法:
string word = "AWord";
string startsWithValue;
if (word.StartsWithAny(new List<string>() { "a", "b", "c" }, out startsWithValue))
{
switch (startsWithValue)
{
case "A":
// Do Something
break;
// etc.
}
}
回答by Daniel M?ller
To return the first character in a string, use:
要返回字符串中的第一个字符,请使用:
Word.Substring(0,1) //where word is a string
回答by DGibbs
Assuming the properties you're checking are string types, you can use the String.StartsWith()method.. for example: -
假设您正在检查的属性是字符串类型,您可以使用String.StartsWith()方法.. 例如:-
if(item.Title.StartsWith("A"))
{
//do whatever
}
Rinse and repeat
冲洗并重复
回答by Greg
You could implement Regular Expressions. They are quite powerful, but when you design your expression it will actually accomplish a task for you.
您可以实现正则表达式。它们非常强大,但是当您设计表达式时,它实际上会为您完成一项任务。
For example finding a number, letter, word, and etc. it is quite expressive and flexible.
例如查找数字、字母、单词等,它非常具有表现力和灵活性。
They have a really great tutorial on them here:
他们在这里有一个非常棒的教程:
An example of such an expression would be:
这种表达式的一个例子是:
string input = "Some additional string to compare against.";
Match match = Regex.Match(input, @"\ba\w*\b", RegexOptions.IgnoreCase);
That would find all the items that start with an "a" no matter the case. You find even utilize Lambda and Linq to make them flow even better.
无论如何,这都会找到以“a”开头的所有项目。您甚至会发现利用 Lambda 和 Linq 使它们流动得更好。
Hopefully that helps.
希望这有帮助。
回答by Apollo SOFTWARE
Try the following below. You can do either StartsWith or Substring 0,1 (first letter)
请尝试以下操作。您可以执行 StartsWith 或 Substring 0,1(第一个字母)
if (Word.Substring(0,1) == "A") {
}
回答by uadrive
You could do something like this to check for a specific character.
您可以执行类似的操作来检查特定字符。
public bool StartsWith(string value, string currentChar) {
return value.StartsWith(currentChar, true, null);
}
The StartsWith method has an option to ignore the case. The third parameter is to set the culture. If null, it just uses the current culture. With this method, you can loop through your words, run the check and process the word to highlight that first character as needed.
StartsWith 方法可以选择忽略大小写。第三个参数是设置文化。如果为 null,则仅使用当前区域性。使用此方法,您可以遍历单词,运行检查并处理单词以根据需要突出显示第一个字符。