[英]C#: How can I only return the first set of capital letter words from a string?
[英]How to get only the first letter of the first 2 words of a long string?
我有一個很長的字符串名稱。 示例: "Ahmet Enes Söylemez"
。 我只想從此數組中獲取"AE"
字母。 我該怎么做?
@{ string result = string.Concat(kullanici.AdSoyad.Where(char.IsUpper));
@result
}
你快到了:Just .Take(2)
string result = string.Concat(kullanici.AdSoyad.Where(char.IsUpper).Take(2));
我建議在這里使用正則表達式並匹配這兩個詞。 如果我們假設單詞 of interes 必須從大寫字母開始並且必須以小寫字母繼續,我們可以這樣實現它:
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
這里我們使用\p{Lu}\p{Ll}+
這是
\p{Lu} - capital letter
\p{Ll}+ - followed by one or more low case letters
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.