F#-删除字符串中第一个之后的重复字符 [英] F# - Remove duplicate characters after first in string

查看:66
本文介绍了F#-删除字符串中第一个之后的重复字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是在字符串中删除特定给定字符的重复项,但保留第一个字符.即:

What I am trying to do is to remove duplicates of a specific given char in a string but letting the first char to remain. I.e:

let myStr = "hi. my .name."

//a function that gets a string and the element to be removed in the string
someFunc myStr "."  

其中someFunc返回如下所示的字符串:

where someFunc returns the string showen as below:

"hi. my name"

从字符串中删除重复项很容易,但是有一种方法可以删除重复项,但让第一个重复的元素保留在字符串中?

It is easy to remove duplicates from a string, but is there a way to remove the duplicates but letting the first duplicated element remain in the string?

推荐答案

这里是一种方法:

let keepFirst c s =
    Seq.mapFold (fun k c' -> (c', k||c<>c'), k&&c<>c') true s
    |> fst
    |> Seq.filter snd
    |> Seq.map fst
    |> Array.ofSeq
    |> System.String

let example = keepFirst '.' "hi. my .name."

这篇关于F#-删除字符串中第一个之后的重复字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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