How to extract only words with first-letter uppercase from a string in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace ConsoleApplication_C_Sharp
{
    static class Program
    {
        static void Main(string[] args)
        {
            string s = "CSharp c c++ Java Python php";

            string[] arr = Regex.Split(s, @"\W");

            var list = new List<string>();
            foreach (string word in arr)
            {
                if (!string.IsNullOrEmpty(word) && char.IsUpper(word[0]))
                    list.Add(word);
            }
            foreach (var w in list)
            {
                Console.WriteLine(w);
            }
        }
    }
}


/*
run:
 
CSharp
Java
Python
  
*/

 



answered Jan 31, 2017 by avibootz
...