简体   繁体   English

优化F#字符串操作

[英]Optimization of F# string manipulation

I am just learning F# and have been converting a library of C# extension methods to F#. 我刚学习F#并且已经将C#扩展方法库转换为F#。 I am currently working on implementing a function called ConvertFirstLetterToUppercase based on the C# implementation below : 我目前正在基于下面C#实现来实现一个名为ConvertFirstLetterToUppercase的函数:

public static string ConvertFirstLetterToUppercase(this string value) {
    if (string.IsNullOrEmpty(value)) return value;
    if (value.Length == 1) return value.ToUpper();
    return value.Substring(0, 1).ToUpper() + value.Substring(1);
}

The F# implementation F#实现

[<System.Runtime.CompilerServices.ExtensionAttribute>]
module public StringHelper
    open System
    open System.Collections.Generic
    open System.Linq

    let ConvertHelper (x : char[]) =  
        match x with
            | [| |] | null -> ""
            | [| head; |] -> Char.ToUpper(head).ToString()
            | [| head; _ |] -> Char.ToUpper(head).ToString() + string(x.Skip(1).ToArray())

    [<System.Runtime.CompilerServices.ExtensionAttribute>]
    let ConvertFirstLetterToUppercase (_this : string) =
        match _this with
        | "" | null -> _this
        | _ -> ConvertHelper (_this.ToCharArray())

Can someone show me a more concise implementation utilizing more natural F# syntax? 有人能用更自然的F#语法向我展示更简洁的实现吗?

open System

type System.String with
    member this.ConvertFirstLetterToUpperCase() =
        match this with
        | null -> null
        | "" -> ""
        | s -> s.[0..0].ToUpper() + s.[1..]

Usage: 用法:

> "juliet".ConvertFirstLetterToUpperCase();;
val it : string = "Juliet"

Something like this? 像这样的东西?

[<System.Runtime.CompilerServices.ExtensionAttribute>]
module public StringHelper = 
[<System.Runtime.CompilerServices.ExtensionAttribute>]
let ConvertFirstLetterToUppercase (t : string) =
    match t.ToCharArray() with
    | null -> t
    | [||] -> t
    | x -> x.[0] <- Char.ToUpper(x.[0]); System.String(x)

Try the following 请尝试以下方法

[<System.Runtime.CompilerServices.ExtensionAttribute>]
module StringExtensions = 
    let ConvertFirstLetterToUpperCase (data:string) =
        match Seq.tryFind (fun _ -> true) data with
        | None -> data
        | Some(c) -> System.Char.ToUpper(c).ToString() + data.Substring(1)

The tryFind function will return the first element for which the lambda returns true . tryFind函数将返回lambda返回true的第一个元素。 Since it always returns true it will simply return the first element or None . 因为它总是返回true,所以它只返回第一个元素或None Once you've established there is at least one element you know data is not null and hence can call Substring 一旦你建立了至少一个元素,你知道data不是null ,因此可以调用Substring

There's nothing wrong with using .NET library functions from a .NET language. 使用.NET语言的.NET库函数没有任何问题。 Maybe a direct translation of your C# extension method is most appropriate, particularly for such a simple function. 也许直接翻译你的C#扩展方法是最合适的,特别是对于这样一个简单的函数。 Although I'd be tempted to use the slicing syntax like Juliet does, just because it's cool. 虽然我很想使用像Juliet那样的切片语法,但仅仅因为它很酷。

open System
open System.Runtime.CompilerServices

[<Extension>]
module public StringHelper =

    [<Extension>]
    let ConvertFirstLetterToUpperCase(this:string) =
        if String.IsNullOrEmpty this then this
        elif this.Length = 1 then this.ToUpper()
        else this.[0..0].ToUpper() + this.[1..]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM