使用 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP