• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 907
  • Last Modified:

Need to pad string

Hi
I have a string say s="99"; I need to pad it left with blanks or characters so I need

s to have the value "  99" for a 4 charcter space pad or "--99" to pad with hyphens. Do not want to write a method. is there any way in String.Format?

thanks
chuck
0
Charles Baldo
Asked:
Charles Baldo
1 Solution
 
openshacCommented:
Try:
string myNumber = "99";
 
string paddedNumber1 = myNumber.PadLeft(4, " ");
string paddedNumber2 = myNumber.PadLeft(4, "-");

Open in new window

0
 
openshacCommented:
Sorry you'll need single quotes.
0
 
BTosonCommented:
s = s.PadLeft(4, "-");
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
Fernando SotoCommented:
Hi charlesbaldo;

This String.Format in the code below will format "99" to be "  99".

String testString = "99";

MessageBox.Show(String.Format("The value{0,4} has a length of 4 with 2 spaces before the value", testString));

Fernando




0
 
Charles BaldoSoftware DeveloperAuthor Commented:
Thanks a ton the single quotes did it.
0
 
Charles BaldoSoftware DeveloperAuthor Commented:
Thank You
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now