469,328 Members | 1,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

How can you format a string to a fixed length?

In 'C' it was possible to format a string to a fixed length, e.g. to 10
characters:

printf("%10.10s","123456789012345"); would return "1234567890", and

printf("%10.10s","12345"); would return " 12345"

I can find no easy way to do this using string.format in C#.

It is possible to set a minumum length e.g.
string.format("{0,10:s}",12345"); will return a string 10 chars long,
but not a maximum - or am I missing something?
Any help welcome.

John.
Jun 28 '07 #1
6 41205
Hello,

I think the Remove method will do a good job here.
int numberofchars = 10;
string test = "dnjghnhnkfdnjkwenfkj";
string only10chars = test.Remove(numberofchars, test.Length-numberofchars);

All the best,

Martin
"John Dinning" wrote:
In 'C' it was possible to format a string to a fixed length, e.g. to 10
characters:

printf("%10.10s","123456789012345"); would return "1234567890", and

printf("%10.10s","12345"); would return " 12345"

I can find no easy way to do this using string.format in C#.

It is possible to set a minumum length e.g.
string.format("{0,10:s}",12345"); will return a string 10 chars long,
but not a maximum - or am I missing something?
Any help welcome.

John.
Jun 28 '07 #2
If I read correctly, the OP wanted a "string.Format()" format
specifier to do this in-place; AFAIK none such exists.

Re getting the trimmed version separately:

string only10chars = test.Substring(0,10);

is probably easier to understand (and therefore debug)...

Marc
Jun 28 '07 #3
Yep,

I forgot to mention that there is no apropriate StringFormat, and of course
Substring "rock" more!

"Marc Gravell" wrote:
If I read correctly, the OP wanted a "string.Format()" format
specifier to do this in-place; AFAIK none such exists.

Re getting the trimmed version separately:

string only10chars = test.Substring(0,10);

is probably easier to understand (and therefore debug)...

Marc
Jun 28 '07 #4
Check out String.PadLeft().

///ark
Jun 28 '07 #5
Thanks for the suggestions.
I was hoping that I had missed something in string.format.
It seems a backward step (more code required) to have to use substring or
remove, but so be it.

Thnaks again,
John,
"Martin#" <Ma****@discussions.microsoft.comwrote in message
news:99**********************************@microsof t.com...
Yep,

I forgot to mention that there is no apropriate StringFormat, and of
course
Substring "rock" more!

"Marc Gravell" wrote:
>If I read correctly, the OP wanted a "string.Format()" format
specifier to do this in-place; AFAIK none such exists.

Re getting the trimmed version separately:

string only10chars = test.Substring(0,10);

is probably easier to understand (and therefore debug)...

Marc

Jun 29 '07 #6
You could make a nice static function with two parameters and a return value.
Then it should akso look clean!

All the best,

Martin

"John Dinning" wrote:
Thanks for the suggestions.
I was hoping that I had missed something in string.format.
It seems a backward step (more code required) to have to use substring or
remove, but so be it.

Thnaks again,
John,
"Martin#" <Ma****@discussions.microsoft.comwrote in message
news:99**********************************@microsof t.com...
Yep,

I forgot to mention that there is no apropriate StringFormat, and of
course
Substring "rock" more!

"Marc Gravell" wrote:
If I read correctly, the OP wanted a "string.Format()" format
specifier to do this in-place; AFAIK none such exists.

Re getting the trimmed version separately:

string only10chars = test.Substring(0,10);

is probably easier to understand (and therefore debug)...

Marc


Jun 29 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

26 posts views Thread by Adrian Parker | last post: by
2 posts views Thread by Greg Lindstrom | last post: by
3 posts views Thread by ecov | last post: by
2 posts views Thread by =?Utf-8?B?Z3Jva25yb2xs?= | last post: by
6 posts views Thread by =?Utf-8?B?VGFydW4=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.