如何将字符串剪切为给定长度的子字符串

时间:2022-02-08 21:41:41

I have string that I want to chop to array of substrings of given length n. I am not interested in remainder (if length of string cannot be divided by n without remainder)

我有字符串,我想切断给定长度n的子串数组。我对余数不感兴趣(如果字符串的长度不能除以n而没有余数)

let ChopString (myString : string) n = 
    let res = 
        seq{ 
            for i = 0 to myString.Length / n - 1 do
                yield( String.sub myString (i*n) n )
            }    
        |> Seq.to_array
    res

This is the best I could do. It looks ugly to me.

这是我能做的最好的事情。它看起来很难看。

Is there nicer/shorter version of this, maybe without for loop?

是否有更好/更短的版本,也许没有for循环?

2 个解决方案

#1


stringInstance.[start..end] is much more readable than String.sub. Here's what I came up with:

stringInstance。[start..end]比String.sub更具可读性。这就是我想出的:


    let chop (input : string) len = 
        seq { for start in 0 .. len .. input.Length - 1
            do yield input.[start..start + len - 1] }
        |> Seq.toArray

Or you can use:

或者您可以使用:


    let chop (input : string) len = 
        Array.init (input.Length / len) (fun index ->
            let start = index * len
            input.[start..start + len - 1])

#2


Craig Stunz left answer here that is now missing. Anyway, he pointed to article about F# that have two functions for string manipulation: explode and implode. These two functions are from standard ML library. Here's the code:

Craig Stunz在这里留下了答案,现在已经失踪了。无论如何,他指的是关于F#的文章,它有两个字符串操作函数:explode和implode。这两个功能来自标准ML库。这是代码:

let rec explode str = 
       let len = String.length str in
           if len=0 then [] else 
              (String.sub str 0 1) :: explode (String.sub str 1 (len-1)) 

let rec implode lst = match lst with
                         []  -> ""
                       | x1 :: x2 -> x1 ^ implode x2

explode chops string into string list where each string is one character.
implode does opposite - concatenates string list into one string.
Both functions are recursive, so it would be interesting to compare performance.

将chops字符串分解为字符串列表,其中每个字符串都是一个字符。 implode相反 - 将字符串列表连接成一个字符串。这两个函数都是递归的,因此比较性能会很有趣。

#1


stringInstance.[start..end] is much more readable than String.sub. Here's what I came up with:

stringInstance。[start..end]比String.sub更具可读性。这就是我想出的:


    let chop (input : string) len = 
        seq { for start in 0 .. len .. input.Length - 1
            do yield input.[start..start + len - 1] }
        |> Seq.toArray

Or you can use:

或者您可以使用:


    let chop (input : string) len = 
        Array.init (input.Length / len) (fun index ->
            let start = index * len
            input.[start..start + len - 1])

#2


Craig Stunz left answer here that is now missing. Anyway, he pointed to article about F# that have two functions for string manipulation: explode and implode. These two functions are from standard ML library. Here's the code:

Craig Stunz在这里留下了答案,现在已经失踪了。无论如何,他指的是关于F#的文章,它有两个字符串操作函数:explode和implode。这两个功能来自标准ML库。这是代码:

let rec explode str = 
       let len = String.length str in
           if len=0 then [] else 
              (String.sub str 0 1) :: explode (String.sub str 1 (len-1)) 

let rec implode lst = match lst with
                         []  -> ""
                       | x1 :: x2 -> x1 ^ implode x2

explode chops string into string list where each string is one character.
implode does opposite - concatenates string list into one string.
Both functions are recursive, so it would be interesting to compare performance.

将chops字符串分解为字符串列表,其中每个字符串都是一个字符。 implode相反 - 将字符串列表连接成一个字符串。这两个函数都是递归的,因此比较性能会很有趣。