By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,838 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,838 IT Pros & Developers. It's quick & easy.

How to convert a string to a fixed lenght string?

P: n/a
I would like to convert a string to a fixed length
e.g. if fixed length = 10
'abc' -> ' abc'
'abcdefghijklm' -> 'abcdefghij'

Is there any simple function in C# can perform this operation?
thanks!
Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi ywchar,

You can use PadLeft and SubString to first add space characters if needed, then chop of anything above 10 characters.

string s = mystring.PadLeft(10, ' ').SubString(0, 10);
On Sun, 26 Jun 2005 19:22:16 +0200, ywchan <yw****@gmail.com> wrote:
I would like to convert a string to a fixed length
e.g. if fixed length = 10
'abc' -> ' abc'
'abcdefghijklm' -> 'abcdefghij'

Is there any simple function in C# can perform this operation?
thanks!


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2

P: n/a
int fixedLength = 10;

string aString = "abcdefghijklm";

if (aString.Length >= fixedLength)

{

aString =
aString.Substring(0,fixedLength);

}

the substring function takes 2 values. the first is where you want to start
in the string. the second is the length of the string to where you want to
go to. So in this case we start at the beggining of the string (0) and we
want to go 10 along. A new string is returnd that is a length of 10, the
rest have been cut off.
"ywchan" <yw****@gmail.com> wrote in message
news:Oo**************@TK2MSFTNGP09.phx.gbl...
I would like to convert a string to a fixed length
e.g. if fixed length = 10
'abc' -> ' abc'
'abcdefghijklm' -> 'abcdefghij'

Is there any simple function in C# can perform this operation?
thanks!

Nov 17 '05 #3

P: n/a
Morten Wennevik wrote:

You can use PadLeft and SubString to first add space characters if
needed, then chop of anything above 10 characters.
string s = mystring.PadLeft(10, ' ').SubString(0, 10);


Um... That will always give you just 10 space in the string. The best
one-liner I could come up with is:

string s = mystring.PadLeft(10, ' ').SubString(mystring.Length > 9 ? 10 :
mystring.Length, 10);

The trick is that the padding it on the left, but the truncation is on the
right.

Usually, both padding & truncation is on the right, in which case, a minor
varaition on yours would work:

string s = mystring.PadRight(10, ' ').SubString(0, 10);
--
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 17 '05 #4

P: n/a
Sorry, I'm not following.

Padding occurs only if the string is less than 10 characters, and to the left of the text as he specified. If padding is necessary, substring will be ignored as the string will be exactly 10 characters.

On Mon, 27 Jun 2005 13:40:27 +0200, James Curran <Ja*********@mvps.org> wrote:
Morten Wennevik wrote:

You can use PadLeft and SubString to first add space characters if
needed, then chop of anything above 10 characters.
string s = mystring.PadLeft(10, ' ').SubString(0, 10);


Um... That will always give you just 10 space in the string. The best
one-liner I could come up with is:

string s = mystring.PadLeft(10, ' ').SubString(mystring.Length > 9 ? 10 :
mystring.Length, 10);

The trick is that the padding it on the left, but the truncation is on the
right.

Usually, both padding & truncation is on the right, in which case, a minor
varaition on yours would work:

string s = mystring.PadRight(10, ' ').SubString(0, 10);


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #5

P: n/a
ooops... I guess I should have look up what PadLeft actually does. I
was assuming that it would always add exactly 10 spaces.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op.ss1gyicgklbvpo@stone...
Sorry, I'm not following.

Padding occurs only if the string is less than 10 characters, and to the left of the text as he specified. If padding is necessary, substring will
be ignored as the string will be exactly 10 characters.
On Mon, 27 Jun 2005 13:40:27 +0200, James Curran <Ja*********@mvps.org> wrote:
Morten Wennevik wrote:

You can use PadLeft and SubString to first add space characters if
needed, then chop of anything above 10 characters.
string s = mystring.PadLeft(10, ' ').SubString(0, 10);


Um... That will always give you just 10 space in the string. The best one-liner I could come up with is:

string s = mystring.PadLeft(10, ' ').SubString(mystring.Length > 9 ? 10 : mystring.Length, 10);

The trick is that the padding it on the left, but the truncation is on the right.

Usually, both padding & truncation is on the right, in which case, a minor varaition on yours would work:

string s = mystring.PadRight(10, ' ').SubString(0, 10);


--
Happy coding!
Morten Wennevik [C# MVP]

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.