所以我有一个字符串,我需要拆分成一个数组,例如:
"1234567890"
我需要能够将其拆分为:
"1234567890".break(2) = ["12345", "67890"]
和
"1234567890".break(3) = ["123", "456","7890"]
所以我不希望它将它分成一个相等部分的数组,我想把字符串拆分成一个长度为n
的数组。你觉得我会怎么做呢?
不,不是这个:regex - Split large string in n-size chunks in JavaScript
答案 0 :(得分:2)
这正是你所要求的
\documentclass{article}
\begin{document}
Some text ....
\begin{knitrout}\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}\begin{alltt}
<<results = "asis", echo = FALSE>>=
source("printHighlighted.R")
data <- seq(from = as.Date("2015-01-15"), by = "day", length.out = 100)
cond <- rep(FALSE, 100)
cond[c(3, 55)] <- TRUE
printHighlighted(x = data, condition = cond, highlight = "\\textcolor{blue}{%s}", width = 60)
@
\end{alltt}\end{kframe}\end{knitrout}
Some text ....
\end{document}
答案 1 :(得分:1)
此功能完成工作:
var brk = function(s,n){
if(s.split && typeof n === "number") {
var l = s.length,
z = ~~(l/n),
zx = (z*n<l) ? l-z*(n-1) : z;
a = [];
for(var i=0; i<n-1; i++) {
a[i] = s.slice(i*z,i*z+z);
}
a[Math.ceil(n-1)] = s.slice(-zx);
return a;
} else {
return false;
}
};
您可以使用控制台日志对其进行测试:
console.log(brk("1234567890",3));