Solved

Add blank spaces, limit spaces

Posted on 2009-05-06
10
383 Views
Last Modified: 2013-12-17
Hi,

I am stumped with this one so any help would be greatly appreciated!

I am building an application to create files in asp.net, c#
I need to be able to enter a string then with a maximum of spaces eg, 20

say in the string i insert 'Education' i then need to pad after with 11 blank spaces.
if the string was 'School' i would need to pad after 14 blank spaces.

This is why pad.right(20) won't work as it will always add 20 spaces no matter what the string.

I have done something similar with numbers but can't work it out with this
*Numbers eg, [string noParcels = string.Format( "{0:d6}", parcels + 1 );] allows max 6 numbers, padding to the left with 0's after number entered.
0
Comment
Question by:kinton
10 Comments
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24316460
Say, you always want your string to be 5 chars wide
pageNo = "00000" + pageNo
Right(pageNo, 5)

if pageNo is '1', then the above will return 00001.
Is this what you are looking for?
0
 
LVL 13

Accepted Solution

by:
copyPasteGhost earned 500 total points
ID: 24316553
This works...

int NumberOfSpaces = 20;
      string test = "Education";
      if (test.Length >= NumberOfSpaces) {
         test = test.Substring(0, NumberOfSpaces);
      }
      else {
         test = test.PadRight(NumberOfSpaces, ' ');
      }
0
 
LVL 22

Expert Comment

by:pivar
ID: 24316636
Hi,

str.PadRight(20) add spaces so you would get spaces padded up to 20 chars. It doesn't limit a string to 20 chars though.

"test".PadRight(20);  would give you "test                "
and

"testtest".PadRight(20);  would give you "testtest            "

/peter
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 2

Author Comment

by:kinton
ID: 24316661
Hi thanks for your quick response.

What is this Right?
It doesn't pick up on my intellisense and is read as an error.

here is the line in my code
0
 
LVL 2

Author Comment

by:kinton
ID: 24316674
sorry didnt see the other solutions, let me just check
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 24316680
PadRight is a string method.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 24316686

"This is why pad.right(20) won't work as it will always add 20 spaces no matter what the string."

No it doesn't http://msdn.microsoft.com/en-us/library/34d75d7s.aspx

yourstring = yourotherString.PadRight(20)

should be fine.


If you also want to handle strings that are too long.

yourstring = String.SubString(yourotherString.PadRight(20), 0, 20);

0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 24316688
If you put a breakpoint on my example you will see it will give you the results you want.

Ghost
0
 
LVL 2

Author Closing Comment

by:kinton
ID: 31578534
Ok this solution does work great! many thanks!
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 24316893
Thanks for the points.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question