我有一个我不太确定的问题.
我的问题如下
let myFunc (text:string) (times:int) = ....
Run Code Online (Sandbox Code Playgroud)
我希望这个函数做的是将字符串放在times参数指定的次数上.
if input = "check " 3 我想要输出字符串= "check check check"
我试过一个循环,但似乎无法使它工作.
任何人?
pad*_*pad 14
实际上该函数已经在String模块中:
let multiply text times = String.replicate times text
Run Code Online (Sandbox Code Playgroud)
要编写自己的函数,一种有效的方法是使用StringBuilder:
open System.Text
let multiply (text: string) times =
let sb = new StringBuilder()
for i in 1..times do
sb.Append(text) |> ignore
sb.ToString()
Run Code Online (Sandbox Code Playgroud)
如果要删除示例中的尾随空格,可以使用类中的Trim()成员String来执行此操作.