473,405 Members | 2,262 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,405 software developers and data experts.

padding a string with blanks

Claus Mygind
571 512MB
How do I add blanks to a string?

I want to evaluate the length of a number (number of digits) and pad the string with blanks depending on the length

The number can be 1 to 6 digits.

1) get the length of the number
2) pad the string with the number of blanks where 6 - string length = number blanks to pad.
For example
123 = "123bbb"
1234 = "1234bb"
12345 = "12345b"
123456 = "123456"

Using the sample code from the online php manual as shown below, I don't get the result they show.

Expand|Select|Wrap|Line Numbers
  1. $job = 12345;
  2. echo 'start-';
  3. echo printf("[%-10s]\n", $job); // left-justification with spaces
  4. echo '-end';
  5.  
This code gives me:
start-[12345 ] 13-end

Not sure why it prints the 13. And I don't see 10 blanks in the result either.
Oct 31 '12 #1

✓ answered by Claus Mygind

I guess the following code is correct, I hope
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $job = 12345;
  3. echo 'start-';
  4. $padLen = str_pad($job, 6);
  5. echo $padLen;
  6. echo '-end';
  7. echo strlen($padLen)."<br>";
  8. echo 'start-';
  9. $padLen = str_pad($job, 10);
  10. echo $padLen;
  11. echo '-end';
  12. echo strlen($padLen)."<br>";
  13.  
  14.  

4 3359
Claus Mygind
571 512MB
I see I made the mistake of adding echo to the response which gave me the 13.

But I want to do further massaging of the string, so I just want to capture the string ie: "1234bb" in a variable not actually streaming it out. So I guess I should not be using "sprintf"
Oct 31 '12 #2
Claus Mygind
571 512MB
I guess the following code is correct, I hope
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $job = 12345;
  3. echo 'start-';
  4. $padLen = str_pad($job, 6);
  5. echo $padLen;
  6. echo '-end';
  7. echo strlen($padLen)."<br>";
  8. echo 'start-';
  9. $padLen = str_pad($job, 10);
  10. echo $padLen;
  11. echo '-end';
  12. echo strlen($padLen)."<br>";
  13.  
  14.  
Oct 31 '12 #3
Dormilich
8,658 Expert Mod 8TB
you might also find sprintf() feasible (look at example #7).

But I want to do further massaging of the string, so I just want to capture the string ie: "1234bb" in a variable not actually streaming it out. So I guess I should not be using "sprintf"
but that’s what sprintf() is doing, it returns a string (into a variable). printf() echoes the string out.
Nov 1 '12 #4
Claus Mygind
571 512MB
Yeah! that one is a little harder for me to wrap my brain around. Example 7 was the one I used in my sample code. The str_pad() was just a little easier to understand and to incorporate into my code, where I want to create a string something like this "123bbbsome text".
(in this example b = space)

This can easily be coded as
$myString = str_pad($numVar, 6).$alphaVar;

Not sure how I would write that with sprintf()?
Nov 2 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
13
by: pb | last post by:
Im wanted to pad out blank spaces with a specific character instead of spaces or zeros, does C support that? printf("$%*d", '*', 5); // Not sure what the format string is supposed to look like...
2
by: John | last post by:
Hi Is there a way in access 97 to pad a string with spaces such that the resultant string is always x characters long? Thanks Regards
2
by: JohnR | last post by:
When creating an msAccess db within the Access UI itself the fields that are text are NOT padded with blanks. For example, if I have a 10 char field and put in "HI" and then when I come back to...
2
by: JohnR | last post by:
When creating an msAccess db within the Access UI itself the fields that are text are NOT padded with blanks. For example, if I have a 10 char field and put in "HI" and then when I come back to...
13
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I...
34
by: Registered User | last post by:
Hi experts, I'm trying to write a program that replaces two or more consecutive blanks in a string by a single blank. Here's what I did: #include <stdio.h> #include <string.h> #define MAX 80
14
by: Richard Maher | last post by:
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'...
10
by: Diego F. | last post by:
Hi all. I have an application that receives a message from a socket in an array from a certain size. As the array size may be longer that the message received, the end of the array has blank...
52
by: marc | last post by:
Hello, Is it possible to right pad with "0" (or other character != blank) ? for example : 1 , length 10 ="1000000000" I've tried with sprintf but I can only left pad with "0" or right pad with...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
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
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.