[英]How to get all words with the same prefix in a list? C#
I am working on a college project and I am stumped on how to get all the words with the prefix ("he") printed using Console.WriteLine.我正在做一个大学项目,我很困惑如何使用 Console.WriteLine 打印所有带有前缀(“he”)的单词。 Here is my code so far, as you can see I already tried but it is highlighting my GetWordsForPrefix("he") in red.
到目前为止,这是我的代码,您可以看到我已经尝试过,但它以红色突出显示了我的 GetWordsForPrefix("he")。 Thank you, (dont mind the other stuff, it is other scenarios I have to implement)
谢谢,(不要介意其他的东西,这是我必须实现的其他场景)
` static void Main(string[] args) { List words = new List(); ` static void Main(string[] args) { List words = new List();
words.Add("car");
words.Add("caramael");
words.Add("hey");
words.Add("hello");
words.Add("helloeverybody");
words.Add("CSC204");
// Console.WriteLine("Scenario 1:");
// Console.WriteLine();
// foreach (string word in words)
// {
// Console.WriteLine(word);
// }
foreach (string word in words.GetWordsForPrefix("he"))
{
Console.WriteLine(word);
}
Console.WriteLine("\nScenario 5 Printing all words:");
words.Sort();
Console.WriteLine();
foreach (string word in words)
{
Console.WriteLine(word);
}
Console.WriteLine("\n Scenario 6 Insert \"car\":");
int index = words.BinarySearch("car");
if (index < 0)
{
words.Insert(~index, "car");
}
Console.WriteLine();
foreach (string word in words)
{
Console.WriteLine("Error, This word already exsits");
}
}
}`
If words is a List, then use LINQ Where().如果 words 是 List,则使用 LINQ Where()。
List<string> words = new List<string>();
words.Add("car");
words.Add("caramael");
words.Add("hey");
words.Add("hello");
words.Add("helloeverybody");
words.Add("CSC204");
var results = words.Where( (x) => x.StartsWith("he"));
It is unclear if you are required to implement a method called GetWordsForPrefix
.目前尚不清楚您是否需要实现名为
GetWordsForPrefix
的方法。 This method is obviously not a method in a List
's properties.该方法显然不是
List
属性中的方法。
Also, as I commented, I am confident you would get an error on the line…另外,正如我评论的那样,我相信您会在线上遇到错误……
List words = new List();
A List
needs a “type” of something. List
需要某种“类型”。 In this case I will assume you need a string
type…在这种情况下,我假设您需要一个
string
类型……
List<string> words = new List<string>();
Then to get all the words in the list that start with “he” could go something like…然后要获得列表中以“他”开头的所有单词,可以 go 之类的东西......
foreach (string word in words) {
if (word.StartsWith("he")) {
Console.WriteLine(word);
}
}
If you are required to implement a GetWordsForPrefix
method, then that method would need a list of all words and a string “prefix.”如果您需要实现
GetWordsForPrefix
方法,那么该方法将需要所有单词的列表和字符串“前缀”。 Then the method would return a list of all the words that start with the given prefix.然后该方法将返回以给定前缀开头的所有单词的列表。 Something like…
就像是…
private static List<string> GetWordsForPrefix(List<string> allWords, string prefix) {
List<string> prefixWords = new List<string>();
foreach (string word in allWords) {
if (word.StartsWith(prefix)) {
prefixWords.Add(word);
}
}
return prefixWords;
}
Then you could call this method in a similar way as your current code.然后,您可以以与当前代码类似的方式调用此方法。 Something like…
就像是…
foreach (string word in GetWordsForPrefix(words, "he")) {
Console.WriteLine(word);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.