shortest way to get first char from every word in a string

var firstChars = str.Split(' ').Select(s => s[0]);

If the performance is critical:

var firstChars = str.Where((ch, index) => ch != ' ' 
                       && (index == 0 || str[index - 1] == ' '));

The second solution is less readable, but loop the string once.


Regular expressions could be the answer:

  Regex.Matches(text, @"\b(\w{1})")
    .OfType<Match>()
    .Select(m => m.Groups[1].Value)
    .ToArray();

string str = "This is my style"; 
str.Split(' ').ToList().ForEach(i => Console.Write(i[0] + " "));

Print first letter of each word in a string

string SampleText = "Stack Overflow Com";
string ShortName = "";
SystemName.Split(' ').ToList().ForEach(i => ShortName += i[0].ToString());  

Output:

SOC

Tags:

C#

String

Split