String Cleaning in C#
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 s开发者_如何学JAVAhould be "This is test".
Can you please help?
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);
}
}
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();
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'
With Linq syntax, you could do something like
return string.Join(' ', from string word in input.Split(' ') where word.Length > 1))
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();
精彩评论