473,471 Members | 4,687 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to convert a string to a fixed lenght string?

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

Similar topics

5
by: bart plessers | last post by:
Hi, I have a counter i Now I want to derive a fixed lenght string from this counter : 1 -> "00000001" 52 -> "00000052" 12896 -> "00012896" What is the fastest way to do this?...
14
by: Spare Change | last post by:
I am told that I can have a dynamic or static string array. So if I declare string dynamic; How do I add elements to dynamic and resize it ?
4
by: Niyazi | last post by:
Hi I am trying to insert some value into SQL Server 2000 tables and I am keep getting the "Input string was not in a correct format" error. When user fills the Form it updates the table call...
7
by: David Meier | last post by:
Hi, I am new to C# and I am facing this small problem: I start a new process using cygwin and I redirect the standard output to a string variable. When I display the string variable in a list...
6
by: Allan Ebdrup | last post by:
How do I easily convert a int to a string? Kind Regards, Allan Ebdrup
2
by: Agnes | last post by:
i use myreader to get the data, and I want to display in the listbox . myReader.item("productname) & " - " & myReader.item("color") Some product name got 15 chars , some is 25 chars. How can I...
8
by: cdolphin88 | last post by:
Hi, I'm reading from a .dat file and I want to know the length of the string . I' using the stringRef.length () but the compiler said there was an error `stringRef' undeclared (first use...
20
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ---->...
4
by: javaalien | last post by:
Could someone please help me how to count lenght string in a file? I have input file like this: 1039387845 35368535615253 695 .... Kindly pls assist me how to count the lenght. Thank you in...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.