本文介绍了填充固定长度的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




任何人都可以告诉我一个正则表达式(或函数或其他)

将字符串填充到固定的字节数?


目前我已经将一个var初始化为N个空格,其中N是

最大字符串我必须填充out,然后我将

的子字符串连接到我的源字符串,以便它以正确的大小结束。这一切都运行良好

但是,如果可能的话,我想要一个更通用/更优雅的制造方法

N个空格。是返回{N} * aString的正则表达式吗?

干杯理查德马赫

解决方案



var lengthRequired = 12

var testString =" test"

while(testString.length< lengthRequired ){

testString + =" " ;;

}

提醒(">" + testString +"<")

-

兰迪

机会有利于准备好的心灵

comp.lang.javascript常见问题 -

Javascript最佳实践 -



var lengthRequired = 12

var testString =" test"

while(testString.length< lengthRequired ){

testString + =" " ;;

}

alert(">" + testString +"<")



好​​吧,嗯,引用你的话,你测试过了吗? :)


-Lost



var lengthRequired = 12
var testString =" test"
while(testString.length< lengthRequired){
testString + =" " ;;
}
提醒(">" + testString +"<")


嗯,嗯,引用你的话, 你测试过了吗? :)



你呢?它看起来有什么问题?

-


Hi,

Can anyone please show me a regular expression (or function or other) for
padding out a string to a fixed number of bytes?

At the moment I''ve got a var initialized to N spaces, where ''N'' is the
largest string I have to pad out, and then I concatenate a substring of that
to my source string so that it ends up the right size. This all works well
but, if possible, I''d like a more generic/elegant approach to manufacturing
N spaces. Is the a regular expression to returning {N}*aString?

Cheers Richard Maher

解决方案

var lengthRequired = 12
var testString = "test"
while (testString.length<lengthRequired){
testString += " ";
}
alert(">" + testString + "<")

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/



var lengthRequired = 12
var testString = "test"
while (testString.length<lengthRequired){
testString += " ";
}
alert(">" + testString + "<")

Well, um, to quote you, "Did you test that?" : )

-Lost



var lengthRequired = 12
var testString = "test"
while (testString.length<lengthRequired){
testString += " ";
}
alert(">" + testString + "<")


Well, um, to quote you, "Did you test that?" : )

Did you? What seems to be wrong with it?
--


这篇关于填充固定长度的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 04:26