.NET method to convert a string to sentence case

后端 未结 9 1732
一向
一向 2020-11-30 09:47

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.

<
9条回答
  •  借酒劲吻你
    2020-11-30 10:09

    A solution in 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)
    

提交回复
热议问题