我如何从这个字符串:"ThisIsMyCapsDelimitedString"
...到这个字符串:"这是我的帽子划分字符串"
VB.net中最少的代码行是首选,但C#也是受欢迎的.
干杯!
我刚才做了这个.它匹配CamelCase名称的每个组件.
/([A-Z]+(?=$|[A-Z][a-z])|[A-Z]?[a-z]+)/g
例如:
"SimpleHTTPServer" => ["Simple", "HTTP", "Server"] "camelCase" => ["camel", "Case"]
要将其转换为仅在单词之间插入空格:
Regex.Replace(s, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 ")
如果你需要处理数字:
/([A-Z]+(?=$|[A-Z][a-z]|[0-9])|[A-Z]?[a-z]+|[0-9]+)/g Regex.Replace(s,"([a-z](?=[A-Z]|[0-9])|[A-Z](?=[A-Z][a-z]|[0-9])|[0-9](?=[^0-9]))","$1 ")
Regex.Replace("ThisIsMyCapsDelimitedString", "(\\B[A-Z])", " $1")
很棒的答案,MizardX!我略微调整它以将数字视为单独的单词,因此"AddressLine1"将变为"地址行1"而不是"地址行1":
Regex.Replace(s, "([a-z](?=[A-Z0-9])|[A-Z](?=[A-Z][a-z]))", "$1 ")
只是为了一点点......这是一个不使用正则表达式的扩展方法.
public static class CamelSpaceExtensions { public static string SpaceCamelCase(this String input) { return new string(Enumerable.Concat( input.Take(1), // No space before initial cap InsertSpacesBeforeCaps(input.Skip(1)) ).ToArray()); } private static IEnumerableInsertSpacesBeforeCaps(IEnumerable input) { foreach (char c in input) { if (char.IsUpper(c)) { yield return ' '; } yield return c; } } }
格兰特瓦格纳的优秀评论不谈:
Dim s As String = RegularExpressions.Regex.Replace("ThisIsMyCapsDelimitedString", "([A-Z])", " $1")
我需要一个支持首字母缩略词和数字的解决方案.这个基于正则表达式的解决方案将以下模式视为单个"单词":
大写字母后跟小写字母
一系列连续数字
连续大写字母(解释为首字母缩略词) - 一个新词可以开始使用最后一个大写,例如HTMLGuide =>"HTML Guide","Theateam"=>"A Team"
你可以做一个单行:
Regex.Replace(value, @"(?更可读的方法可能更好:
using System.Text.RegularExpressions; namespace Demo { public class IntercappedStringHelper { private static readonly Regex SeparatorRegex; static IntercappedStringHelper() { const string pattern = @" (?这是(XUnit)测试的摘录:
[Theory] [InlineData("PurchaseOrders", "Purchase-Orders")] [InlineData("purchaseOrders", "purchase-Orders")] [InlineData("2Unlimited", "2-Unlimited")] [InlineData("The2Unlimited", "The-2-Unlimited")] [InlineData("Unlimited2", "Unlimited-2")] [InlineData("222Unlimited", "222-Unlimited")] [InlineData("The222Unlimited", "The-222-Unlimited")] [InlineData("Unlimited222", "Unlimited-222")] [InlineData("ATeam", "A-Team")] [InlineData("TheATeam", "The-A-Team")] [InlineData("TeamA", "Team-A")] [InlineData("HTMLGuide", "HTML-Guide")] [InlineData("TheHTMLGuide", "The-HTML-Guide")] [InlineData("TheGuideToHTML", "The-Guide-To-HTML")] [InlineData("HTMLGuide5", "HTML-Guide-5")] [InlineData("TheHTML5Guide", "The-HTML-5-Guide")] [InlineData("TheGuideToHTML5", "The-Guide-To-HTML-5")] [InlineData("TheUKAllStars", "The-UK-All-Stars")] [InlineData("AllStarsUK", "All-Stars-UK")] [InlineData("UKAllStars", "UK-All-Stars")]