473,666 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16455
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.len gth<lengthRequi red){
testString += " ";
}
alert(">" + testString + "<")

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript 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******** ************@gi ganews.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.len gth<lengthRequi red){
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******* *************@g iganews.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.len gth<lengthRequi red){
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.co m...
-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.len gth<lengthRequi red){
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.javas cript:
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.len gth<lengthRequi red){
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('te st',12);
alert('>' + a + '< ' + a.length);

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

a = fixedLength('te st',-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******** ************@gi ganews.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.len gth<lengthRequi red){
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.javas cript 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******** ************@gi ganews.com...
-Lost said the following on 4/14/2007 12:31 AM:
>"Randy Webb" <Hi************ @aol.comwrote in message
news:4L******* *************@g iganews.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.len gth<lengthRequi red){
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******** ************@gi ganews.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.len gth<lengthRequi red){
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.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 14 '07 #9
Hi Evertjan,

"Evertjan." <ex************ **@interxnl.net wrote in message
news:Xn******** ************@19 4.109.133.242.. .
Randy Webb wrote on 14 apr 2007 in comp.lang.javas cript:
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.len gth<lengthRequi red){
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('te st',12);
alert('>' + a + '< ' + a.length);

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

a = fixedLength('te st',-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

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

Similar topics

1
3523
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 not IE. Would someone please take a look at this and tell me if it can made to work in IE? If it can, a hint on the syntax/code would be much appreciated! PS, this would also get rid of my fixed background. Thanks in advance Jaz
4
4284
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 <div id="padding0">
1
2439
by: Lalit Bhatia | last post by:
how can we fix first column in DataGrid? Regards, Lalit Bhatia
0
1450
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 message news:OQ0wkCeZFHA.3220@TK2MSFTNGP14.phx.gbl... > Hi Lalit,
7
3645
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 stored procedure that I'm trying to edit to make the front-end app display the right data. The relevant part of the stored procedure that I'm working on is as follow:
31
3088
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 like the middle column just to use up that space instead. Any way this is possible? Thanks for your help, it is highly appreciated!
3
2206
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, and the whole setup works exactly the way I want it to in firefox. Link to site in question However, in IE there are several problems; the major problem is that, instead of layering the two divs on top of each other, IE puts the later-created...
5
3182
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 I am doing the right thing ... I think. But what unit should I use for padding and margin? EM? PIXEL?
3
5514
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 information to show as displace. please help me. my coding are as below: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>...
12
2590
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 THIS DOES NOT WORK! Recipe: About this recipe, and the lovely blurbage that makes people want to know more. I see it as rattling garbage`.
0
8445
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.