Question about string.Format function

G

Guest

Hi All:
I need an easy way to format a string.
Here is my situation:
when the user inputs a character 'A', my logic suppose adding two ##. The
expecting result is ##A.
when the user inputs two characters 'AA', my logic only needs to add one #.
The expecting result is #AA.
when the user inputs three characters 'AAA', my logic does not need to add
any #.

I am expecting the issue can be resolve easily in string.Format() function
with proper formatter. But the most of formatter I can find is relate to
numeric or datetime. I'd like to know is there formatter I can use to help me
resolve my issue.

Thanks in advance
 
W

Wouter Ballet

Hi David,

There's actually an even easier solution...

userInput.PadLeft(3,'#');

with userInput being the string to hold "A", "AA", or "AAA"

Kind Regards,

Wouter
 
M

Morten Wennevik

Hi David,

No need to use String.Format.

string s = GetUserInput();
s = s.PadLeft(3, '#');
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top