使用C#正则表达式打印字符串中每个单词的第一个字母
假设我们的字符串是-
string str = "The Shape of Water got an Oscar Award!";
使用以下正则表达式显示每个单词的首字母-
@"\b[a-zA-Z]"
这是完整的代码-
示例
using System;
using System.Text.RegularExpressions;
namespace RegExApplication {
public class Program {
private static void showMatch(string text, string expr) {
Console.WriteLine("The Expression: " + expr);
MatchCollection mc = Regex.Matches(text, expr);
foreach (Match m in mc) {
Console.WriteLine(m);
}
}
public static void Main(string[] args) {
string str = "The Shape of Water got an Oscar Award!";
Console.WriteLine("Display first letter of each word!");
showMatch(str, @"\b[a-zA-Z]");
}
}
}输出结果
Display first letter of each word! The Expression: \b[a-zA-Z] T S o W g a O A
热门推荐
5 拳友的祝福语简短
10 鼠年父母祝福语简短的
11 顾客出院祝福语简短精辟
12 退任学长祝福语简短
13 生日离职祝福语简短独特
14 校园周年庆祝福语简短
15 元旦祝福语20秒简短
16 追星女孩的祝福语简短
17 鲜花卡片简短的祝福语
18 古语祝福语的句子简短