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

3 Answers

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

public class Program
{
    public static void Main(string[] args)
    {
        string s = "CSharp c cpp Java Python php Go rust";
 
        string[] arr = Regex.Split(s, @"\W");
 
        var list = new List<string>();
        foreach (string word in arr) {
            if (!string.IsNullOrEmpty(word) && char.IsLower(word[0])) {
                list.Add(word);
            }
        }    
        
        foreach (var w in list) {
            Console.WriteLine(w);
        }
    }
}



 
/*
run:
   
c
cpp
php
rust
    
*/

 



answered Feb 2, 2017 by avibootz
edited Apr 13, 2024 by avibootz
0 votes
using System;
using System.Collections.Generic;
 
public class program
{
    internal static List<string> extract_only_words_with_first_letter_lowercase(string s) {
        List<string> words = new List<string>();
 
        int start = 0;
        int end;
        while ((end = s.IndexOf(" ", start, StringComparison.Ordinal)) != -1) {
            string word = s.Substring(start, end - start);
 
            if (char.IsLower(word[0])) {
                words.Add(word);
            }
            start = end + 1;
        }
 
        if (char.IsLower(s.Substring(start)[0])) {
            words.Add(s.Substring(start));
        }
 
        return new List<string>(words);
    }
 
    public static void Main(string[] args)
    {
        string s = "C# is a General-purpose high-level pRogramming language";
 
        List<string> words = extract_only_words_with_first_letter_lowercase(s);
 
        foreach (string w in words) {
            Console.WriteLine(w);
        }
    }
}
 
 
 
/*
run:
    
is
a
high-level
pRogramming
language
    
*/

 



answered Apr 13, 2024 by avibootz
0 votes
using System;
using System.Collections.Generic;
  
public class program
{
    internal static List<string> extract_only_words_with_first_letter_lowercase(string s) {
        List<string> lowercase = new List<string>();
         
        string[] words = s.Split(' ');
  
        foreach (var word in words) {
            if (char.IsLower(word[0])) {
                lowercase.Add(word);
            }
        }
         
        return new List<string>(lowercase);
    }
  
    public static void Main(string[] args)
    {
        string s = "C# is a General-purpose high-level pRogramming language";
  
        List<string> words = extract_only_words_with_first_letter_lowercase(s);
  
        foreach (string w in words) {
            Console.WriteLine(w);
        }
    }
}
  
  
  
/*
run:
     
is
a
high-level
pRogramming
language
     
*/

 

 



answered Apr 13, 2024 by avibootz
...