472,327 Members | 1,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,327 software developers and data experts.

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 41369
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Adrian Parker | last post by:
I'm using the code below in my project. When I print all of these fixed length string variables, one per line, they strings in questions do not...
2
by: Greg Lindstrom | last post by:
Hello- I'm creating fixed-length record layouts for various record translations I need to perform. I have been using a home-grown object,...
3
by: ecov | last post by:
Is there any easy way to read into a dataset a file that was created from a BCP command. The file is in a fixed length format. I would also like...
1
by: Rick Knospler | last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for the most part except for the fixed length strings and fixed length string...
2
by: danishce | last post by:
Hello: I want to format fixed length string using vb.net. If the size of string is less than field size then it should filled with zero. ex:i want...
1
by: kendrick82 | last post by:
Hi, I would like to seek some advise and assistance regarding the following matter as I am new to VB.Net. I'll appreciate any helps render. I...
2
by: =?Utf-8?B?Z3Jva25yb2xs?= | last post by:
Is there a way that I can define a WebMethod with a parameter that is a fixed length string? I'm using VB.Net 2005 and would like to define a...
5
by: Rowan | last post by:
Hi, I am somewhat new to .net and c#. (What I learned in previous co has to be unlearned). I am doing something that seems simple but I think...
6
by: =?Utf-8?B?VGFydW4=?= | last post by:
Hi, I have to generate a fixed length text file.I have a file formats like fields details as well as its length and position.what actually i have...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.