Solved

Add blank spaces, limit spaces

Posted on 2009-05-06
10
387 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

636 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