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 41524
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
properly pad with 0s. strQuantity prints as " ...
|
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,
"FixedLengthRecord" for about 4 years now and am very...
|
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 to be able to write out a dataset to the same type...
|
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
arrays. Bascially the vb6 programmer stored all...
|
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 to format fixed length 10 digit amount field which...
|
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 am developing a VB application using VB.Net 2003 to...
|
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 webmethod
that will prevent the caller from passing 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 there
is a better way than how I learned to do it. ...
|
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 to do
that i will get the data from the databas...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |