473,395 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Padding fixed-length strings

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
Apr 14 '07 #1
14 16432
Richard Maher said the following on 4/13/2007 10:35 PM:
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?
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/
Apr 14 '07 #2
"Randy Webb" <Hi************@aol.comwrote in message
news:4L********************@giganews.com...
Richard Maher said the following on 4/13/2007 10:35 PM:
>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?

var lengthRequired = 12
var testString = "test"
while (testString.length<lengthRequired){
testString += " ";
}
alert(">" + testString + "<")
Well, um, to quote you, "Did you test that?" : )

-Lost
Apr 14 '07 #3
Lee
-Lost said:
>
"Randy Webb" <Hi************@aol.comwrote in message
news:4L********************@giganews.com...
>Richard Maher said the following on 4/13/2007 10:35 PM:
>>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?

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?
--

Apr 14 '07 #4
"Lee" <RE**************@cox.netwrote in message news:ev********@drn.newsguy.com...
-Lost said:
>>
"Randy Webb" <Hi************@aol.comwrote in message
news:4L********************@giganews.com...
>>Richard Maher said the following on 4/13/2007 10:35 PM:
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?

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?
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
Apr 14 '07 #5
Randy Webb wrote on 14 apr 2007 in comp.lang.javascript:
Richard Maher said the following on 4/13/2007 10:35 PM:
>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?

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>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 14 '07 #6
-Lost said the following on 4/14/2007 12:31 AM:
"Randy Webb" <Hi************@aol.comwrote in message
news:4L********************@giganews.com...
>Richard Maher said the following on 4/13/2007 10:35 PM:
>>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?
var lengthRequired = 12
var testString = "test"
while (testString.length<lengthRequired){
testString += " ";
}
alert(">" + testString + "<")

Well, um, to quote you, "Did you test that?" : )
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.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 14 '07 #7
"Randy Webb" <Hi************@aol.comwrote in message
news:Se********************@giganews.com...
-Lost said the following on 4/14/2007 12:31 AM:
>"Randy Webb" <Hi************@aol.comwrote in message
news:4L********************@giganews.com...
>>Richard Maher said the following on 4/13/2007 10:35 PM:
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?
var lengthRequired = 12
var testString = "test"
while (testString.length<lengthRequired){
testString += " ";
}
alert(">" + testString + "<")

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

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
Apr 14 '07 #8
-Lost said the following on 4/14/2007 11:18 AM:
"Randy Webb" <Hi************@aol.comwrote in message
news:Se********************@giganews.com...
>-Lost said the following on 4/14/2007 12:31 AM:
>>"Randy Webb" <Hi************@aol.comwrote in message
news:4L********************@giganews.com...
Richard Maher said the following on 4/13/2007 10:35 PM:
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?
var lengthRequired = 12
var testString = "test"
while (testString.length<lengthRequired){
testString += " ";
}
alert(">" + testString + "<")
Well, um, to quote you, "Did you test that?" : )
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.
Testing it further in IE and FF2.0 I get some results that I wouldn't
expect where the length of testString is 16 instead of 12 (and I know
why) so I should tested it more thoroughly. Oh well :)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 14 '07 #9
Hi Evertjan,

"Evertjan." <ex**************@interxnl.netwrote in message
news:Xn********************@194.109.133.242...
Randy Webb wrote on 14 apr 2007 in comp.lang.javascript:
Richard Maher said the following on 4/13/2007 10:35 PM:
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?
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>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
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
Apr 14 '07 #10
Richard Maher wrote on 15 apr 2007 in comp.lang.javascript:

I wrote:
>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

;-)

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 15 '07 #11
In comp.lang.javascript message <yZydnS--i_vh5r3bnZ2dnUVZ_qemnZ2d@comcas
t.com>, Sat, 14 Apr 2007 02:58:34, -Lost <mi*********@comcast.net>
posted:
>
I wonder why Firefox will not allow multiple spaces in an alert() then?
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#MLSshows how to make a
padding-string of any length L by using only O(log2(L)) concatenations.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zipTimo Salmi's Turbo Pascal FAQ.
Apr 15 '07 #12
On Apr 15, 9:53 am, Dr J R Stockton <j...@merlyn.demon.co.ukwrote:
In comp.lang.javascript message <yZydnS--i_vh5r3bnZ2dnUVZ_qemnZ2d@comcas
t.com>, Sat, 14 Apr 2007 02:58:34, -Lost <missed-s...@comcast.net>
posted:
I wonder why Firefox will not allow multiple spaces in an alert() then?

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#MLSshows 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.

--
../rh

Apr 15 '07 #13
In comp.lang.javascript message <11**********************@b75g2000hsg.go
oglegroups.com>, Sun, 15 Apr 2007 12:54:39, ro********@gmail.com posted:
>On Apr 15, 9:53 am, Dr J R Stockton <j...@merlyn.demon.co.ukwrote:
>Noting Evertjan's reply -
<URL:http://www.merlyn.demon.co.uk/js-misc0.htm#MLSshows 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.
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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Apr 16 '07 #14
On Apr 16, 5:14 am, Dr J R Stockton <j...@merlyn.demon.co.ukwrote:
In comp.lang.javascript message <1176666879.166040.209...@b75g2000hsg.go
oglegroups.com>, Sun, 15 Apr 2007 12:54:39, ron.h.h...@gmail.com posted:
On Apr 15, 9:53 am, Dr J R Stockton <j...@merlyn.demon.co.ukwrote:
Noting Evertjan's reply -
<URL:http://www.merlyn.demon.co.uk/js-misc0.htm#MLSshows 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.

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.

--
../rh

Apr 17 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jaz | last post by:
Trying to use a fixed layer for a couple of NAV buttons. I found this code, but the IE part is commented, and I don't understand the IF statement. It works great on Moz/Firebird and Opera BUT...
4
by: Wilhelm Kutting | last post by:
hi, when i use the padding-left attribut, i like to overwrite a default value like that ..padding30 {margin-left: 30px;} ..padding0 {margin-left: 0px;} <div class="padding30"> Padding 30...
1
by: Lalit Bhatia | last post by:
how can we fix first column in DataGrid? Regards, Lalit Bhatia
0
by: Lalit Bhatia | last post by:
thanks for the answer but I need it for windows forms. I have a desktop application where I need to implement this. -- Regards, Lalit Bhatia "Mona" <mona@discussions.microsoft.com> wrote in...
7
by: Astra | last post by:
Hi All Can you please help me with a few queries on adding a header line and padding rows out. I apologise profusely for not providing the DDL for this, but I don't have it. All I have is...
31
by: Sarita | last post by:
Hello, this might sound stupid, but I got a really nice homepage template which unfortunately is a 3-Column Fixed Width CSS format. Now I don't have any content for the right column and would...
3
by: Avaenuha | last post by:
I have a banner-and-sidebar menu that I want to remain always-accessible. So I've made them both position:fixed, and used z-index to ensure the layers work properly. Both the CSS and HTML validate,...
5
by: shapper | last post by:
Hello, I always use EM for text size, letter spacing and line heights. For DIV widths I usually use PIXELS if fixed width web site and PERCENT if fluid layout web site. I think in both cases...
3
by: Noorain | last post by:
I designed a site. i want to header,footer,left & right column fixed but body information only scrolling. this site screen to be 800/600 px. i designed this way but when i used position fixed all...
12
tpgames
by: tpgames | last post by:
Okay, I admit I was lousy at DIV tags before HTML5. However, 4.01 is leaving us, and I'm making a new site, and wanted to do it right in HTML5/CSS3. Nothing fancy, just wanted... THIS content ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.