Solved

Add blank spaces, limit spaces

Posted on 2009-05-06
10
381 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

929 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now