.NET方法将字符串判的情况下转换 [英] .NET method to convert a string to sentence case

查看:160
本文介绍了.NET方法将字符串判的情况下转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找到一个文本字符串,它是大写的,以SentenceCase转换功能。所有的例子我能找到把文成首字母大写。

I'm looking for a function to convert a string of text that is in UpperCase to SentenceCase. All the examples I can find turn the text into TitleCase.

句的情况下在一般意义上
  描述的方式,大写
  在一个句子中使用。句子
  案例还描述标准
  英语句子的资本,
  即句子的第一个字母
  是大写的,其余为
  小写(除非要求
  资本的具体原因,
  例如专有名词,首字母缩写,等等)。

Sentence case in a general sense describes the way that capitalization is used within a sentence. Sentence case also describes the standard capitalization of an English sentence, i.e. the first letter of the sentence is capitalized, with the rest being lower case (unless requiring capitalization for a specific reason, e.g. proper nouns, acronyms, etc.).

任何人都可以点我在脚本或功能SentenceCase的方向是什么?

Can anyone point me in the direction of a script or function for SentenceCase?

推荐答案

有没有什么内置于.NET - 不过,这是其中的情况下,常规的前pression处理实际可能效果较好之一。我会通过首先把整个字符串为小写开始,然后,作为第一近似值,你可以使用正则表达式来找到像所有序列[AZ] \\。\\ S +(。),并使用 ToUpper的()来捕获组转换为大写。在正则表达式类有一个重载的替换(),它接受方法 MatchEvaluator 委托,它允许您定义如何更换匹配的值。

There isn't anything built in to .NET - however, this is one of those cases where regular expression processing actually may work well. I would start by first converting the entire string to lower case, and then, as a first approximation, you could use regex to find all sequences like [a-z]\.\s+(.), and use ToUpper() to convert the captured group to upper case. The RegEx class has an overloaded Replace() method which accepts a MatchEvaluator delegate, which allows you to define how to replace the matched value.

下面是这个在工作中code例如:

Here's a code example of this at work:

var sourcestring = "THIS IS A GROUP. OF CAPITALIZED. LETTERS.";
// start by converting entire string to lower case
var lowerCase = sourcestring.ToLower();
// matches the first sentence of a string, as well as subsequent sentences
var r = new Regex(@"(^[a-z])|\.\s+(.)", RegexOptions.ExplicitCapture);
// MatchEvaluator delegate defines replacement of setence starts to uppercase
var result = r.Replace(lowerCase, s => s.Value.ToUpper());

// result is: "This is a group. Of uncapitalized. Letters."

这可以在许多不同的方式加以完善,以更好地匹配更广泛的各种句型(不只是那些在信+周期结束)。

This could be refined in a number of different ways to better match a broader variety of sentence patterns (not just those ending in a letter+period).

这篇关于.NET方法将字符串判的情况下转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆