我正在尝试编写一个函数,当输入采用包含单词的字符串并删除所有单个字符单词并返回没有删除字符的新字符串
例如:
string news = FunctionName("This is a test");
//'news' here should be "This is test".
Run Code Online (Sandbox Code Playgroud)
你能帮忙吗?
强制性LINQ单线程:
string.Join(" ", "This is a test".Split(' ').Where(x => x.Length != 1).ToArray())
Run Code Online (Sandbox Code Playgroud)
或者作为更好的扩展方法:
void Main()
{
var output = "This is a test".WithoutSingleCharacterWords();
}
public static class StringExtensions
{
public static string WithoutSingleCharacterWords(this string input)
{
var longerWords = input.Split(' ').Where(x => x.Length != 1).ToArray();
return string.Join(" ", longerWords);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1727 次 |
| 最近记录: |