Padding fixed-length strings

R

Richard Maher

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
 
L

-Lost

Randy Webb said:
Richard Maher said the following on 4/13/2007 10:35 PM:

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

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

-Lost
 
L

-Lost

Lee said:
-Lost said:

Did you? What seems to be wrong with it?

You know, I did. I also however, forgot to test in something other than Firefox.

I wonder why Firefox will not allow multiple spaces in an alert() then?

-Lost
 
E

Evertjan.

Randy Webb wrote on 14 apr 2007 in comp.lang.javascript:
Richard Maher said the following on 4/13/2007 10:35 PM:

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

<script type='text/javascript'>

// Without looping, with cutoff of too long strings
// IE7 and FF tested

function fixedLength(s,n){
n = (n<0) ?0 :n
s = s.substr(0,n);
var a = [];
a.length = n - s.length + 1;
return s + a.join(' ');
};

// testing:

a = fixedLength('test',12);
alert('>' + a + '< ' + a.length);

a = fixedLength('test',3);
alert('>' + a + '< ' + a.length);

a = fixedLength('test',-22);
alert('>' + a + '< ' + a.length);

</script>
 
L

-Lost

Randy Webb said:
-Lost said the following on 4/14/2007 12:31 AM:

Yes I did. And I get the results I expected in FF, IE and Opera. Did *you* test it? And
if you got different results, what were they and what OS/Browser gave them? There is a
reason I added the > and < though.

In Firefox 1.5.0.11 on Windows XP SP2, I get:
test < (notice only one space)

As Lee suggested, I ended up testing in Opera and Internet Explorer (6, not 7) and noticed
that they both allow spaces in the alert() string. Firefox will not give me more than
one.

I actually remembered coming across this situation before, that is why when I saw it, I
tested it in Firefox (to make sure I remembered it correctly), noticed it did not pad
spaces, then posted. Sorry for not checking it more thoroughly. Then my question would
have just been, "Hey, why does that not work in Firefox? At least for me..."

Thanks!

-Lost
 
R

Richard Maher

Hi Evertjan,

Evertjan. said:
Randy Webb wrote on 14 apr 2007 in comp.lang.javascript:
Richard Maher said the following on 4/13/2007 10:35 PM:

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

<script type='text/javascript'>

// Without looping, with cutoff of too long strings
// IE7 and FF tested

function fixedLength(s,n){
n = (n<0) ?0 :n
s = s.substr(0,n);
var a = [];
a.length = n - s.length + 1;
return s + a.join(' ');
};

// testing:

a = fixedLength('test',12);
alert('>' + a + '< ' + a.length);

a = fixedLength('test',3);
alert('>' + a + '< ' + a.length);

a = fixedLength('test',-22);
alert('>' + a + '< ' + a.length);

</script>

I like it. I'll use the Array length/join(' ') at initialize time as a sort
of space$() function to preallocate a maximum space-filled string, but then
stick to substringing out of that the number of space bytes I need for
padding. Thanks for the help.

Cheers Richard Maher
 
E

Evertjan.

Richard Maher wrote on 15 apr 2007 in comp.lang.javascript:
function fixedLength(s,n){
n = (n<0) ?0 :n
s = s.substr(0,n);
var a = [];
a.length = n - s.length + 1; // never negative <--------------
return s + a.join(' ');
};
I like it. I'll use the Array length/join(' ') at initialize time as a
sort of space$() function to preallocate a maximum space-filled
string, but then stick to substringing out of that the number of space
bytes I need for padding.

Did some testing:

alert(new Array(17).length); // 17
alert(new Array(17).join('?').length); // 16

alert(new Array('17').length); // 1 <-----------------
alert(new Array('17').join('?').length); // 2 <-------------

alert(new Array(0).length); // 0
alert(new Array(0).join('?').length); // 0
// alert(new Array(-3).length); // error


function newArray(n){
var a = [];
a.length = n;
return a;
}

alert(newArray(17).length); // 17
alert(newArray(17).join('?').length); // 16

alert(newArray('17').length); // 17 <------------------
alert(newArray('17').join('?').length); // 16 <-------------

alert(newArray(0).length); // 0
alert(newArray(0).join('?').length); // 0
// alert(newArray(-3).length); // error

;-)
 
R

ron.h.hall

In comp.lang.javascript message <yZydnS--i_vh5r3bnZ2dnUVZ_qemnZ2d@comcas
t.com>, Sat, 14 Apr 2007 02:58:34, -Lost <[email protected]>
posted:




Because it is out-of-date? Here, FF 2.0.0.3 shows multiple spaces,
though they are difficult to count.

Noting Evertjan's reply -
<URL:http://www.merlyn.demon.co.uk/js-misc0.htm#MLS> shows how to make a
padding-string of any length L by using only O(log2(L)) concatenations.

Not as given though. "&&" appears where "&" was likely the intended
operator.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Sun, 15 Apr 2007 12:54:39, (e-mail address removed) posted:
Not as given though. "&&" appears where "&" was likely the intended
operator.

Thank you. I think that must be a typo on transferring from the
"development" version - you'll have noted that the result is not
affected, only the speed. Corrected.
 
R

ron.h.hall

In comp.lang.javascript message <[email protected]
oglegroups.com>, Sun, 15 Apr 2007 12:54:39, (e-mail address removed) posted:



Thank you. I think that must be a typo on transferring from the
"development" version - you'll have noted that the result is not
affected, only the speed. Corrected.

Yes, I could have been clearer that I was making reference only to the
failure on O(log2(L) efficiency.

While your BigCat function is instructive with regard to recursion, I
personally prefer to see the use of Array.prototype.join mechanism for
facilitating padding operations, both for simplicity and efficiency
reasons.

I've generally also found that use of built-in functions where
possible (executing as native code, and presumably with efficient
algorithms) are generally considerably faster than script-implemented
alternatives.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top