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. 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.
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
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
Check out String.PadLeft().
///ark
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
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...
|
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...
|
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.
...
|
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...
|
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...
| |