English 中文(简体)
。 NET方法:将缓刑改为判决
原标题:.NET method to convert a string to sentence case

I m 寻求一项功能,将位于上Case的系列案文转换成SentenceCase。 我可以把案文变成标题Case。

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.).

是否有任何人向我提出文字或功能要求,以了解情况。

最佳回答

这里没有任何东西。 然而,在正规的言论自由处理实际上可能运转良好的情况下,网络就是这种情况。 首先,我要将整条脚体改为下级,然后,作为第一近似,你可以使用reg,以找到所有顺序,如<代码>[a-z].s+(,并使用<代码>ToUpper(),将被俘群体改为上下级。

这里是一部工作守则:

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."

可以用多种不同方式加以改进,以更好地适应更广泛的服刑模式(而不仅仅是在字母+期间结束的刑期)。

问题回答

这为我工作。

/// <summary>
/// Converts a string to sentence case.
/// </summary>
/// <param name="input">The string to convert.</param>
/// <returns>A string</returns>
public static string SentenceCase(string input)
{
    if (input.Length < 1)
        return input;

    string sentence = input.ToLower();
    return sentence[0].ToString().ToUpper() +
       sentence.Substring(1);
}

在<代码>ToTitleCase()中建立了一套功能,这一功能将在今后扩大到支持多种文化。

例 管理网:

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] values = { "a tale of two cities", "gROWL to the rescue",
                          "inside the US government", "sports and MLB baseball",
                          "The Return of Sherlock Holmes", "UNICEF and children"};

      TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
      foreach (var value in values)
         Console.WriteLine("{0} --> {1}", value, ti.ToTitleCase(value));
   }
}
// The example displays the following output:
//    a tale of two cities --> A Tale Of Two Cities
//    gROWL to the rescue --> Growl To The Rescue
//    inside the US government --> Inside The US Government
//    sports and MLB baseball --> Sports And MLB Baseball
//    The Return of Sherlock Holmes --> The Return Of Sherlock Holmes
//    UNICEF and children --> UNICEF And Children

虽然这总体上是有用的,但有一些重要的限制:

Generally, title casing converts the first character of a word to uppercase and the rest of the characters to lowercase. However, this method does not currently provide proper casing to convert a word that is entirely uppercase, such as an acronym. The following table shows the way the method renders several strings.

...the ToTitleCase method provides an arbitrary casing behavior which is not necessarily linguistically correct. A linguistically correct solution would require additional rules, and the current algorithm is somewhat simpler and faster. We reserve the right to make this API slower in the future.

资料来源:http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase.aspx

如果你想要判刑,就会有除短时间外的旁观:

string input = "THIS IS YELLING! WHY ARE WE YELLING? BECAUSE WE CAN. THAT IS ALL.";
var sentenceRegex = new Regex(@"(^[a-z])|[?!.:,;]s+(.)", RegexOptions.ExplicitCapture);
input = sentenceRegex.Replace(input.ToLower(), s => s.Value.ToUpper());

如果你的投入不是一句,而是许多句子,就成为一个非常困难的问题。

定期表达将证明是一种宝贵的工具,但(1) 你们必须非常了解这些表述是否有效,(2) 他们可能无法独自从事这项工作。

考虑本句

史密斯先生作了回答。

本句从一封信开始,在中间有一位数字、各种图解、适当的名称和<代码>。

复杂性是巨大的,这是一句话。

使用RegEx最重要的事之一是“了解你的数据”。 如果你知道你所服的刑罚种类之广,你的任务将更加可行。

不管怎样,在你对结果感到满意之前,你不得不对你的执行情况表示担忧。 我建议撰写一些自动测试,并附上一些样本投入——在你执行时,你可以定期进行测试,了解你在什么地方重新接近,以及你在哪些地方仍然没有标识。

这是我使用的。 在<>最大><>条件下工作,包括:

  • multiple sentences
  • sentences beginning and ending with spaces
  • 判决从A-Z以外的性质开始。 例如,它将努力“如果你想要100.00美元,那么我就问了”。

    <Extension()>
    Public Function ToSentanceCase(ByVal s As String) As String
          Written by Jason. Inspired from: http://www.access-programmers.co.uk/forums/showthread.php?t=147680
    
        Dim SplitSentence() As String = s.Split(".")
    
        For i = 0 To SplitSentence.Count - 1
            Dim st = SplitSentence(i)
    
            If st.Trim = "" Or st.Trim.Count = 1 Then Continue For   ignore empty sentences or sentences with only 1 character.
    
              skip past characters that are not A-Z, 0-9 (ASCII) at start of sentence.
            Dim y As Integer = 1
            Do Until y > st.Count
                If (Asc(Mid(st, y, 1)) >= 65 And Asc(Mid(st, y, 1)) <= 90) Or _
                      (Asc(Mid(st, y, 1)) >= 97 And Asc(Mid(st, y, 1)) <= 122) Or _
                     (Asc(Mid(st, y, 1)) >= 48 And Asc(Mid(st, y, 1)) <= 57) Then
                    GoTo Process
                Else
                    Dim w = Asc(Mid(st, y, 1))
                    y += 1
                End If
            Loop
            Continue For
    
    Process:
            Dim sStart As String = ""
            If y > 1 Then sStart = Left(st, 0 + (y - 1))
    
            Dim sMid As String = UCase(st(y - 1))   capitalise the first non-space character in sentence.
    
            Dim sEnd As String = Mid(st, y + 1, st.Length)
    
            SplitSentence(i) = sStart & sMid & sEnd
    
        Next
    
          rejoin sentances back together:
        Dim concat As String = ""
        For Each st As String In SplitSentence
            concat &= st & "."
        Next
    
        concat = concat.TrimEnd(1)
    
        Return concat
    
    End Function
    

但是,就适当的传票和缩略语而言,......总是会出现英语的情况,因为图解并不简单。 例如,这本书在Chris House附近发现了一只短片(......)。

彻底解决这一问题,你将需要提出一个字典,说明所有可能的缩略语/图语,并随时更新字典! 在考虑这一点之后,大多数人将乐于作出妥协,否则只是使用微软Word。

F#的解决办法:

open System

let proper (x : string) =
    x.Split(   )
    |> Array.filter ((<>) "")
    |> Array.map (fun t ->
        let head = Seq.head t |> Char.ToUpper |> string
        let tail = Seq.tail t |> Seq.map (Char.ToLower >> string)
        Seq.append [head] tail
        |> Seq.reduce (fun acc elem -> acc + elem))
    |> Array.reduce (fun acc elem -> acc + " " + elem)
public string GetSentenceCase(string ReqdString) {
    string StrInSentCase = "";
    for (int j = 0; j < ReqdString.Length; j++) {
        if (j == 0) {
           StrInSentCase = ReqdString.ToString().Substring(j, 1).ToUpper();
        }
        else {
            StrInSentCase = StrInSentCase + ReqdString.ToString().Substring(j, 1).ToLower();
        }
    }
    return StrInSentCase.ToString();
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签