skip to Main Content

I have a long string name. Example: "Ahmet Enes Söylemez". I want to get only "AE" letters from this array. How can I do it ?

@{ string result = string.Concat(kullanici.AdSoyad.Where(char.IsUpper));
@result
 }

2

Answers


  1. I suggest using Regular Expressions here and match these two words. If we assume that the word of interes must start from capital letter and must continue with low case leters we can implement it it like this:

    using System.Linq;
    using System.Text.RegularExpressions;
    
    ...
    
    string text = "Ahmet Enes Söylemez";
    
    string letters = string.Concat(Regex 
      .Matches(text, @"p{Lu}p{Ll}+") // match each word of interest
      .Cast<Match>()
      .Take(2)       // We want just two first matches
      .Select(match => match.Value[0])); // Initial letters from each match
    

    Here we use p{Lu}p{Ll}+ which is

    p{Lu}  - capital letter
    p{Ll}+ - followed by one or more low case letters
    

    Fiddle

    Login or Signup to reply.
  2. You’re almost there: Just .Take(2)

    string result = string.Concat(kullanici.AdSoyad.Where(char.IsUpper).Take(2));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search