C#中的字符串清理

mou*_*iec 3 c# string

我正在尝试编写一个函数,当输入采用包含单词的字符串并删除所有单个字符单词并返回没有删除字符的新字符串

例如:

string news = FunctionName("This is a test");
//'news' here should be "This is test".
Run Code Online (Sandbox Code Playgroud)

你能帮忙吗?

Mar*_*ris 6

强制性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)