如何将这个字符串进行转换:"ThisIsMyCapsDelimitedString",转成 "This Is My Caps Delimited String"?
最好能用 VB.net 写出最简洁的代码,当然 C# 也可以。
祝好!
最好能用 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")
HtmlTag
的例子,即使在每种情况下HTML都应该是大写的,而ANZAC则不同。然而,它对于两级缩写做出了例外,以IOStream
作为示例(用于IO流)。 - Arthur Tacca很棒的回答,MizardX!我稍微修改了一下,将数字视为单独的单词,这样"AddressLine1"就会变成"Address Line 1"而不是"Address Line1"。
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 IEnumerable<char> InsertSpacesBeforeCaps(IEnumerable<char> 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")
我需要一个支持缩写词和数字的解决方案。这个基于正则表达式的解决方案将以下模式视为单独的“单词”:
你可以把它做成一行代码:
Regex.Replace(value, @"(?<!^)((?<!\d)\d|(?(?<=[A-Z])[A-Z](?=[a-z])|[A-Z]))", " $1")
一个更易读的方法可能更好:
using System.Text.RegularExpressions;
namespace Demo
{
public class IntercappedStringHelper
{
private static readonly Regex SeparatorRegex;
static IntercappedStringHelper()
{
const string pattern = @"
(?<!^) # Not start
(
# Digit, not preceded by another digit
(?<!\d)\d
|
# Upper-case letter, followed by lower-case letter if
# preceded by another upper-case letter, e.g. 'G' in HTMLGuide
(?(?<=[A-Z])[A-Z](?=[a-z])|[A-Z])
)";
var options = RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled;
SeparatorRegex = new Regex(pattern, options);
}
public static string SeparateWords(string value, string separator = " ")
{
return SeparatorRegex.Replace(value, separator + "$1");
}
}
}
以下是(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")]
为了更多样化,使用普通的C#对象,以下内容产生与@MizardX优秀的正则表达式相同的输出。
public string FromCamelCase(string camel)
{ // omitted checking camel for null
StringBuilder sb = new StringBuilder();
int upperCaseRun = 0;
foreach (char c in camel)
{ // append a space only if we're not at the start
// and we're not already in an all caps string.
if (char.IsUpper(c))
{
if (upperCaseRun == 0 && sb.Length != 0)
{
sb.Append(' ');
}
upperCaseRun++;
}
else if( char.IsLower(c) )
{
if (upperCaseRun > 1) //The first new word will also be capitalized.
{
sb.Insert(sb.Length - 1, ' ');
}
upperCaseRun = 0;
}
else
{
upperCaseRun = 0;
}
sb.Append(c);
}
return sb.ToString();
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
var examples = new List<string> {
"THEQuickBrownFox",
"theQUICKBrownFox",
"TheQuickBrownFOX",
"TheQuickBrownFox",
"the_quick_brown_fox",
"theFOX",
"FOX",
"QUICK"
};
foreach (var example in examples)
{
Console.WriteLine(ToTitleCase(example));
}
}
private static string ToTitleCase(string example)
{
var fromSnakeCase = example.Replace("_", " ");
var lowerToUpper = Regex.Replace(fromSnakeCase, @"(\p{Ll})(\p{Lu})", "$1 $2");
var sentenceCase = Regex.Replace(lowerToUpper, @"(\p{Lu}+)(\p{Lu}\p{Ll})", "$1 $2");
return new CultureInfo("en-US", false).TextInfo.ToTitleCase(sentenceCase);
}
}
THE Quick Brown Fox The QUICK Brown Fox The Quick Brown FOX The Quick Brown Fox The Quick Brown Fox The FOX FOX QUICK
string s = "ThisIsMyCapsDelimitedString";
string t = Regex.Replace(s, "([A-Z])", " $1").Substring(1);
正则表达式比简单循环慢约10-12倍:
public static string CamelCaseToSpaceSeparated(this string str)
{
if (string.IsNullOrEmpty(str))
{
return str;
}
var res = new StringBuilder();
res.Append(str[0]);
for (var i = 1; i < str.Length; i++)
{
if (char.IsUpper(str[i]))
{
res.Append(' ');
}
res.Append(str[i]);
}
return res.ToString();
}
Regex.Replace(s, "([A-Z0-9]+)", " $1").Trim()
。如果你想在每个大写字母处拆分,只需删除加号。 - Mladen B.