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! 5 23678
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.PadLef t(10, ' ').SubString(0, 10);
On Sun, 26 Jun 2005 19:22:16 +0200, ywchan <yw****@gmail.c om> 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]
int fixedLength = 10;
string aString = "abcdefghijklm" ;
if (aString.Length >= fixedLength)
{
aString =
aString.Substri ng(0,fixedLengt h);
}
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.c om> wrote in message
news:Oo******** ******@TK2MSFTN GP09.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!
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.PadLef t(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.PadLef t(10, ' ').SubString(my string.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.PadRig ht(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
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*********@mv ps.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.PadLef t(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.PadLef t(10, ' ').SubString(my string.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.PadRig ht(10, ' ').SubString(0, 10);
--
Happy coding!
Morten Wennevik [C# MVP]
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.ss1gyic gklbvpo@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*********@mv ps.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.PadLef t(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.PadLef t(10, ' ').SubString(my string.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.PadRig ht(10, ' ').SubString(0, 10);
-- Happy coding! Morten Wennevik [C# MVP] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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?
This is part of a recursive subroutine, so the command will be executed
|
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 ?
|
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 tbl_04_Hellenic.
Then In that form some of data also goes and fills another table call
tbl_LEHTAR.
Here is my code:
|
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 box
I see those squares representing UNIX line breaks. How can I convert
those to Windows style line breaks?
Thanks. Dave.
|
by: Allan Ebdrup |
last post by:
How do I easily convert a int to a string?
Kind Regards,
Allan Ebdrup
| |
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 display it in fixed lenght. e.g 30 chars ?
I want my data display like
TOYS RED
TELEPPHONE BLUE
....... RED
^^^^^^^^^^^^^^^^
FIXED COLUMN.
|
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 this function)
|
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 ----> 0001
2 ----> 0002
|
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 adv.
String lineA = "";
BufferedReader inputA = new BufferedReader (new FileReader("A.txt"));
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |