.NET方法将字符串判的情况下转换

编程入门 行业动态 更新时间:2024-10-28 02:31:50
本文介绍了.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方法将字符串判的情况下转换

本文发布于:2023-10-25 22:33:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528308.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   情况下   方法   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!