C# 。我需要在每一行中找到最短的单词(来自文件)
C# . I need to find shortest words in every lines(from file)
我需要在每一行中找到最短的单词,条件是这些单词不能短于 "x"
例如这是数据:
It was popularised in the 1960s with the release of Letraset sheets
containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum
如果我写 x=5;
结果应该是:
Lorem
Aldus
Lorem
我知道如何找到最长的单词,仅此而已,但最短的条件却不知道:
static string Longest(string line, char[] s, int x)
{
string[] parts =line.Split(s,StringSplitOptions.RemoveEmptyEntries);
string longg = "";
foreach (string word in parts)
if (word.Length >=longg.Length)
longg = word;
return longg;
}
您可以为此使用 Linq。
return parts.Where(w => w.Length >= x).OrderBy(w => w.Length).FirstOrDefault();
将为您提供长度至少为 x
或 null
如果 none 符合要求的最短单词。如果你想要一个空字符串而不是 null
,你可以在末尾添加 ?? string.Empty
。
这将检查每个单词,并将 return 大于或等于 x
且小于 shortWord
的单词。
static string Shortest(string line, char[] s, int x)
{
string[] parts =line.Split(s,StringSplitOptions.RemoveEmptyEntries);
string shortWord = "";
foreach (string word in parts)
if (word.Length >= x && (shortWord.Length == 0 || word.Length < shortWord.Length))
shortWord = word;
return shortWord;
}
更新
这是一个接受单词和字母数组的函数,return是单词中存在的第一个字母。
static char GetLetter(string word, char[] letters)
{
foreach(var c in word)
if (letters.Contains(c)) return c;
return '[=11=]'; // return a default letter if no letters was found.
}
你可以这样称呼它 (GetLetter("Lorem", new char[] {'a', 'e', 'o'})
)。当你得到那个字母时(即 o
in "Lorem"
),你可以找到包含该字母的所有单词,如下所示:
List<string> result = new List<string>();
foreach (var word in line)
{
if (word.Contains(c)) result.Add(word); // c is the letter
}
return result;
我需要在每一行中找到最短的单词,条件是这些单词不能短于 "x"
例如这是数据:
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
如果我写 x=5;
结果应该是:
Lorem
Aldus
Lorem
我知道如何找到最长的单词,仅此而已,但最短的条件却不知道:
static string Longest(string line, char[] s, int x)
{
string[] parts =line.Split(s,StringSplitOptions.RemoveEmptyEntries);
string longg = "";
foreach (string word in parts)
if (word.Length >=longg.Length)
longg = word;
return longg;
}
您可以为此使用 Linq。
return parts.Where(w => w.Length >= x).OrderBy(w => w.Length).FirstOrDefault();
将为您提供长度至少为 x
或 null
如果 none 符合要求的最短单词。如果你想要一个空字符串而不是 null
,你可以在末尾添加 ?? string.Empty
。
这将检查每个单词,并将 return 大于或等于 x
且小于 shortWord
的单词。
static string Shortest(string line, char[] s, int x)
{
string[] parts =line.Split(s,StringSplitOptions.RemoveEmptyEntries);
string shortWord = "";
foreach (string word in parts)
if (word.Length >= x && (shortWord.Length == 0 || word.Length < shortWord.Length))
shortWord = word;
return shortWord;
}
更新
这是一个接受单词和字母数组的函数,return是单词中存在的第一个字母。
static char GetLetter(string word, char[] letters)
{
foreach(var c in word)
if (letters.Contains(c)) return c;
return '[=11=]'; // return a default letter if no letters was found.
}
你可以这样称呼它 (GetLetter("Lorem", new char[] {'a', 'e', 'o'})
)。当你得到那个字母时(即 o
in "Lorem"
),你可以找到包含该字母的所有单词,如下所示:
List<string> result = new List<string>();
foreach (var word in line)
{
if (word.Contains(c)) result.Add(word); // c is the letter
}
return result;