C# 将字符串转换为标题大小写
我需要将以下内容转换为标题案例:C# 将字符串转换为标题大小写,c#,C#,我需要将以下内容转换为标题案例: 短语中的第一个单词 换句话说,在同一短语中,长度大于minLength 我在看,但结果不是预期的 因此,minLength=2的短语“汽车非常快”将变成“汽车非常快” 快” 我能够使用以下命令生成第一个大写单词: Char[] letters = source.ToCharArray(); letters[0] = Char.ToUpper(letters[0]); 为了得到我所说的话: Regex.Matches(source, @"\b(\w|['-])+
Char[] letters = source.ToCharArray();
letters[0] = Char.ToUpper(letters[0]);
为了得到我所说的话:
Regex.Matches(source, @"\b(\w|['-])+\b"
但我不知道如何把这些放在一起
谢谢,,
米格尔示例代码:
string input = "i have the car which is very fast";
int minLength = 2;
string regexPattern = string.Format(@"^\w|\b\w(?=\w{{{0}}})", minLength);
string output = Regex.Replace(input, regexPattern, m => m.Value.ToUpperInvariant());
更新(适用于单个字符串中有多个句子的情况)
您可能希望处理代码>、?
和其他符号,然后可以使用以下符号。您可以添加任意数量的句子终止符号
string input = "i have the car which is very fast! me is slow.";
int minLength = 2;
string regexPattern = string.Format(@"(?<=(^|[.!?])\s*)\w|\b\w(?=\w{{{0}}})", minLength);
string output = Regex.Replace(input, regexPattern, m => m.Value.ToUpperInvariant());
一种不需要正则表达式的替代(且简单)解决方案是使用String.Split方法和列表。选择函数映射复杂条件:
var text = @"i have the car which is very fast. me is slow.";
var length = 2;
var first = true; // first word in the sentence
var containsDot = false; // previous word contains a dot
var result = text
.Split(' ')
.ToList()
.Select (p =>
{
if (first)
{
p = FirstCharToUpper(p);
first = false;
}
if (containsDot)
{
p = FirstCharToUpper(p);
containsDot = false;
}
containsDot = p.Contains(".");
if (p.Length > length)
{
return FirstCharToUpper(p);
}
return p;
})
.Aggregate ((h, t) => h + " " + t);
Console.WriteLine(result);
输出为:
I Have The Car Which is Very Fast. Me is Slow.
firstChartUpper
方法如下:
此解决方案的缺点是:条件越复杂,select语句就越复杂/不可读,但它是正则表达式的替代品。英文标题大小写极其复杂。而且它是不可计算的。句号
你能得到的最好的方法是根据你的喜好列表改变所有的小词。对于所有的口头表达,这仍然是错误的。虽然扩展的变体列表可以捕获其中的许多变体,但是如果没有语义分析,仍然无法确定其中的一些变体。两个例子:
- 空车上运行/空车上运行
- 在建筑物上工作
后者确实从上下文中变得清晰;前者并非如此。意思上有明显的区别,但计算机无法决定哪个是对的
(有时甚至人类也不能。我问了StackExchnge论坛的第一个例子,但没有得到可接受的答案。)
以下是我喜欢的替代品清单;但有些四个字母的单词(不是双关语)是个人的选择。还有一些人可能会争辩说,所有类型的数字,像任何、所有、很少的数字一样,都应该大写
这一点也不优雅,事实上这是一种尴尬。但它对我来说效果相当好,所以我定期使用它,并通过它提供了10多万个标题
public string ETC(string title)
{ // english title capitalization
if (title == null) return "";
string s = title.Trim().Replace('`', '\''); // change apo to tick mark
TextInfo UsaTextInfo = new CultureInfo("en-US", false).TextInfo;
s = UsaTextInfo.ToTitleCase(s); // caps for all words
// a list of exceptions one way or the other..
s = s.Replace(" A ", " a ");
s = s.Replace(" also ", " Also ");
s = s.Replace(" An ", " an ");
s = s.Replace(" And ", " and ");
s = s.Replace(" as ", " As ");
s = s.Replace(" At ", " at ");
s = s.Replace(" be ", " Be ");
s = s.Replace(" But ", " But ");
s = s.Replace(" By ", " by ");
s = s.Replace(" For ", " for ");
s = s.Replace(" From ", " from ");
s = s.Replace(" if ", " If ");
s = s.Replace(" In ", " in ");
s = s.Replace(" Into ", " into ");
s = s.Replace(" he ", " He ");
s = s.Replace(" has ", " Has ");
s = s.Replace(" had ", " Had ");
s = s.Replace(" is ", " Is ");
s = s.Replace(" my ", " My ");
s = s.Replace(" ", " "); // no triple spaces
s = s.Replace("'N'", "'n'"); // Rock 'n' Roll
s = s.Replace("'N'", "'n'"); // Rock 'n Roll
s = s.Replace(" no ", " No ");
s = s.Replace(" Nor ", " nor ");
s = s.Replace(" Not ", " not ");
s = s.Replace(" Of ", " of ");
s = s.Replace(" Off ", " off ");
s = s.Replace(" On ", " on ");
s = s.Replace(" Onto ", " onto ");
s = s.Replace(" Or ", " or ");
s = s.Replace(" O'c ", " O'C ");
s = s.Replace(" Over ", " over ");
s = s.Replace(" so ", " So ");
s = s.Replace(" To ", " to ");
s = s.Replace(" that ", " That ");
s = s.Replace(" this ", " This ");
s = s.Replace(" thus ", " Thus ");
s = s.Replace(" The ", " the ");
s = s.Replace(" Too ", " too ");
s = s.Replace(" when ", " When ");
s = s.Replace(" With ", " with ");
s = s.Replace(" Up ", " up ");
s = s.Replace(" Yet ", " yet ");
// a few(!) verbal expressions
s = s.Replace(" Get up ", " Get Up ");
s = s.Replace(" Give up ", " Give Up ");
s = s.Replace(" Givin' up ", " Givin' Up ");
s = s.Replace(" Grow up ", " Grow Up ");
s = s.Replace(" Hung up ", " Hung Up ");
s = s.Replace(" Make up ", " Make Up ");
s = s.Replace(" Wake Me up ", " Wake Me Up ");
s = s.Replace(" Mixed up ", " Mixed Up ");
s = s.Replace(" Shut up ", " Shut Up ");
s = s.Replace(" Stand up ", " Stand Up ");
s = s.Replace(" Wind up ", " Wind Up ");
s = s.Replace(" Wake up ", " Wake Up ");
s = s.Replace(" Come up ", " Come Up ");
s = s.Replace(" Working on ", " Working On ");
s = s.Replace(" Waiting on ", " Waiting On ");
s = s.Replace(" Turn on ", " Turn On ");
s = s.Replace(" Move on ", " Move On ");
s = s.Replace(" Keep on ", " Keep On ");
s = s.Replace(" Bring It on ", " Bring It On ");
s = s.Replace(" Hold on ", " Hold On ");
s = s.Replace(" Hang on ", " Hang On ");
s = s.Replace(" Go on ", " Go On ");
s = s.Replace(" Coming on ", " Coming On ");
s = s.Replace(" Come on ", " Come On ");
s = s.Replace(" Call on ", " Call On ");
s = s.Replace(" Trust in ", " Trust In ");
s = s.Replace(" Fell in ", " Fell In ");
s = s.Replace(" Falling in ", " Falling In ");
s = s.Replace(" Fall in ", " Fall In ");
s = s.Replace(" Faith in ", " Faith In ");
s = s.Replace(" Come in ", " Come In ");
s = s.Replace(" Believe in ", " Believe In ");
return s.Trim();
}
注意,仍然有很多规则不能像这样实现
有些基本规则并不难:第一个和最后一个单词大写。所有动词(Is)、形容词(Red)、promouns(He)、名词(Ace)和数字(One),即使它们的字母少于3个(或4个)
但例外情况很难,例如:当介词是动词表达的一部分或一个时,不要大写
例1:“在建筑物上工作”-你必须知道这是一首福音歌,才能决定它是“开着的”
示例2:“正在运行/空运行”。可能表示“正在运行”或“正在运行(使用气体指示器)”或“空”
因此,最终您将不得不接受折衷。以下是一种使用StringBuilder
和纯字符串方法的方法,不需要使用正则表达式,因此它应该非常有效:
public static string ToTitleCase(string input, int minLength = 0)
{
TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
string titleCaseDefault = ti.ToTitleCase(input);
if (minLength == 0)
return titleCaseDefault;
StringBuilder sb = new StringBuilder(titleCaseDefault.Length);
int wordCount = 0;
char[] wordSeparatorChars = " \t\n.,;-:".ToCharArray();
for (int i = 0; i < titleCaseDefault.Length; i++)
{
char c = titleCaseDefault[i];
bool nonSpace = !char.IsWhiteSpace(c);
if (nonSpace)
{
wordCount++;
int firstSpace = titleCaseDefault.IndexOfAny(wordSeparatorChars, i);
int endIndex = firstSpace >= 0 ? firstSpace : titleCaseDefault.Length;
string word = titleCaseDefault.Substring(i, endIndex - i);
if (wordCount == 1) // first word upper
sb.Append(word);
else
sb.Append(word.Length < minLength ? word.ToLower() : ti.ToTitleCase(word));
i = endIndex - 1;
}
else
sb.Append(c);
}
return sb.ToString();
}
现在试过了吗?我刚刚更新了我的问题以显示我试过的内容。如果minLength为2,为什么“
太短而不能用标题大小写?@TimSchmelter Cause OP陈述了哪个长度高于minLength
@UlugbekUmirov:所以minLength
实际上不够min;)如何将您的示例扩展到所有长度大于2的单词,以及第一个单词和点后的单词,即使它们的长度小于2?我进行了测试,它似乎可以正常工作,只有一个例外。如果我有一个像“e-marketing”这样的词,我希望它变成“e-marketing”,在这一刻我得到了“e-marketing”。用你的例子能解决这个问题吗?感谢You@MDMoura添加了-
@MDMoura的处理,不应该是正确的标题案例“电子营销”(类似于“电子商务”)?@elgonzo没有严格的规则:
I Have The Car Which is Very Fast. Me is Slow.
public static string FirstCharToUpper(string input)
{
if (String.IsNullOrEmpty(input))
throw new ArgumentException("ARGH!");
return input.First().ToString().ToUpper() + String.Join("", input.Skip(1));
}
public string ETC(string title)
{ // english title capitalization
if (title == null) return "";
string s = title.Trim().Replace('`', '\''); // change apo to tick mark
TextInfo UsaTextInfo = new CultureInfo("en-US", false).TextInfo;
s = UsaTextInfo.ToTitleCase(s); // caps for all words
// a list of exceptions one way or the other..
s = s.Replace(" A ", " a ");
s = s.Replace(" also ", " Also ");
s = s.Replace(" An ", " an ");
s = s.Replace(" And ", " and ");
s = s.Replace(" as ", " As ");
s = s.Replace(" At ", " at ");
s = s.Replace(" be ", " Be ");
s = s.Replace(" But ", " But ");
s = s.Replace(" By ", " by ");
s = s.Replace(" For ", " for ");
s = s.Replace(" From ", " from ");
s = s.Replace(" if ", " If ");
s = s.Replace(" In ", " in ");
s = s.Replace(" Into ", " into ");
s = s.Replace(" he ", " He ");
s = s.Replace(" has ", " Has ");
s = s.Replace(" had ", " Had ");
s = s.Replace(" is ", " Is ");
s = s.Replace(" my ", " My ");
s = s.Replace(" ", " "); // no triple spaces
s = s.Replace("'N'", "'n'"); // Rock 'n' Roll
s = s.Replace("'N'", "'n'"); // Rock 'n Roll
s = s.Replace(" no ", " No ");
s = s.Replace(" Nor ", " nor ");
s = s.Replace(" Not ", " not ");
s = s.Replace(" Of ", " of ");
s = s.Replace(" Off ", " off ");
s = s.Replace(" On ", " on ");
s = s.Replace(" Onto ", " onto ");
s = s.Replace(" Or ", " or ");
s = s.Replace(" O'c ", " O'C ");
s = s.Replace(" Over ", " over ");
s = s.Replace(" so ", " So ");
s = s.Replace(" To ", " to ");
s = s.Replace(" that ", " That ");
s = s.Replace(" this ", " This ");
s = s.Replace(" thus ", " Thus ");
s = s.Replace(" The ", " the ");
s = s.Replace(" Too ", " too ");
s = s.Replace(" when ", " When ");
s = s.Replace(" With ", " with ");
s = s.Replace(" Up ", " up ");
s = s.Replace(" Yet ", " yet ");
// a few(!) verbal expressions
s = s.Replace(" Get up ", " Get Up ");
s = s.Replace(" Give up ", " Give Up ");
s = s.Replace(" Givin' up ", " Givin' Up ");
s = s.Replace(" Grow up ", " Grow Up ");
s = s.Replace(" Hung up ", " Hung Up ");
s = s.Replace(" Make up ", " Make Up ");
s = s.Replace(" Wake Me up ", " Wake Me Up ");
s = s.Replace(" Mixed up ", " Mixed Up ");
s = s.Replace(" Shut up ", " Shut Up ");
s = s.Replace(" Stand up ", " Stand Up ");
s = s.Replace(" Wind up ", " Wind Up ");
s = s.Replace(" Wake up ", " Wake Up ");
s = s.Replace(" Come up ", " Come Up ");
s = s.Replace(" Working on ", " Working On ");
s = s.Replace(" Waiting on ", " Waiting On ");
s = s.Replace(" Turn on ", " Turn On ");
s = s.Replace(" Move on ", " Move On ");
s = s.Replace(" Keep on ", " Keep On ");
s = s.Replace(" Bring It on ", " Bring It On ");
s = s.Replace(" Hold on ", " Hold On ");
s = s.Replace(" Hang on ", " Hang On ");
s = s.Replace(" Go on ", " Go On ");
s = s.Replace(" Coming on ", " Coming On ");
s = s.Replace(" Come on ", " Come On ");
s = s.Replace(" Call on ", " Call On ");
s = s.Replace(" Trust in ", " Trust In ");
s = s.Replace(" Fell in ", " Fell In ");
s = s.Replace(" Falling in ", " Falling In ");
s = s.Replace(" Fall in ", " Fall In ");
s = s.Replace(" Faith in ", " Faith In ");
s = s.Replace(" Come in ", " Come In ");
s = s.Replace(" Believe in ", " Believe In ");
return s.Trim();
}
public static string ToTitleCase(string input, int minLength = 0)
{
TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
string titleCaseDefault = ti.ToTitleCase(input);
if (minLength == 0)
return titleCaseDefault;
StringBuilder sb = new StringBuilder(titleCaseDefault.Length);
int wordCount = 0;
char[] wordSeparatorChars = " \t\n.,;-:".ToCharArray();
for (int i = 0; i < titleCaseDefault.Length; i++)
{
char c = titleCaseDefault[i];
bool nonSpace = !char.IsWhiteSpace(c);
if (nonSpace)
{
wordCount++;
int firstSpace = titleCaseDefault.IndexOfAny(wordSeparatorChars, i);
int endIndex = firstSpace >= 0 ? firstSpace : titleCaseDefault.Length;
string word = titleCaseDefault.Substring(i, endIndex - i);
if (wordCount == 1) // first word upper
sb.Append(word);
else
sb.Append(word.Length < minLength ? word.ToLower() : ti.ToTitleCase(word));
i = endIndex - 1;
}
else
sb.Append(c);
}
return sb.ToString();
}
string text = "the car is very fast";
string output = ToTitleCase(text, 3);