在 Mathematica 中创建一个可变长度的空格字符串 [英] Creating a variable length string of whitespaces in Mathematica

查看:21
本文介绍了在 Mathematica 中创建一个可变长度的空格字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下 Mathematica 函数 f 创建一个长度为 n 的空格字符串.

The following Mathematica function f creates a string of whitespace of length n.

f[n_]:=Fold[StringJoin,"",Array[" "&,n]]

必须有无数的选择来创建这个函数.

There must be a zillion alternatives to create this function.

你会怎么做?

推荐答案

f[n_] := StringJoin @ ConstantArray[" ", n]

因为@ 和@@ 一样惯用,而且速度更快(感谢 Mr.Wizard 进行基准测试)而且更短,所以我更新了解决方案.

since @ is as idiomatic as @@ and a bit faster (thanks to Mr.Wizard for benchmarking) and shorter i updated the solution.

这篇关于在 Mathematica 中创建一个可变长度的空格字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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