tags:

views:

112

answers:

5

I am trying to write a function that as input takes a string containing words and removes all single character words and returns the new string without the removed characters

E.g.:

string news = FunctionName("This is a test");
//'news' here should be "This is test".

Can you please help?

+3  A: 

I'm sure there's a nicer answer using regex, but you could do the following:

string[] words = news.Split(' ');

StringBuilder builder = new StringBuilder();
foreach (string word in words)
{
    if (word.Length > 1)
    {
       if (builder.ToString().Length ==0)
       {
           builder.Append(word);
       }
       else
       {
           builder.Append(" " + word);
       }
    }
}

string result = builder.ToString();
GenericTypeTea
+2  A: 

The interesting thing about this question is that presumably you also want to remove one of the spaces surrounding the single-letter word.

    string[] oldText = {"This is a test", "a test", "test a"};
    foreach (string s in oldText) {

        string newText = Regex.Replace(s, @"\s\w\b|\b\w\s", string.Empty);
        WL("'" + s + "' --> '" + newText + "'");
    }

Output...

'This is a test' --> 'This is test'
'a test' --> 'test'
'test a' --> 'test'
Ed Guiness
MPritch
At least, move instancing the regex out of the loop for Increased Performance(tm) ;)
Peter Lillevold
Coded for clarity with optimisations left as an exercise for the reader ;-)
Ed Guiness
A: 

With Linq syntax, you could do something like

return string.Join(' ', from string word in input.Split(' ') where word.Length > 1))
tdammers
+6  A: 

Obligatory LINQ one-liner:

string.Join(" ", "This is a test".Split(' ').Where(x => x.Length != 1).ToArray())

Or as a nicer extension method:

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);
    }
}
Martin Harris
+1 like the use of the extension method and how much cleaner it is than my example. I must learn LINQ properly.
GenericTypeTea
what if there is different whitespace between words?
Grzenio
@Grzenio You could use Regex.Split(input, @"\s") instead if you wanted to catch tabs and linebreaks too. I was just passing the test case from the question :)
Martin Harris
A: 
string str = "This is a test.";
var result = str.Split(' ').Where(s => s.Length > 1).Aggregate((s, next) => s + " " + next);

UPD

Using the extension method:

public static string RemoveSingleChars(this string str)
{
      return str.Split(' ').Where(s => s.Length > 1).Aggregate((s, next) => s + " " + next);       
}


//----------Usage----------//


var str = "This is a test.";
var result = str.RemoveSingleChars();
Eugene Cheverda