I need to generate a text line with a fixed lenght:
What I have right now is:
StringBuilder _sb = new StringBuilder();
_sb.Append(string.Format("{0,5}", "MM")); // serie to cancel
_sb.Append(string.Format("{0,20}", "45444")); // folio to cancel
_sb.Append(string.Format("{0,30}", "AC1122")); // account number (optional)
This works great because generates a fixed lenght string of 55 characters.
The issue comes when for example the optional value is a empty string like:
StringBuilder _sb = new StringBuilder();
_sb.Append(string.Format("{0,5}", "MM")); // serie to cancel
_sb.Append(string.Format("{0,20}", "45444")); // folio to cancel
_sb.Append(string.Format("{0,30}", "")); // account number (optional)
Having empty string inside the string.format then wont give a fixed length, and I still need to have a 30 chars length.
Any clue is well appreciated!!
Thanks
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.
You can use PadLeft
method:
StringBuilder _sb = new StringBuilder();
_sb.Append("MM".PadLeft(5)); // serie to cancel
_sb.Append("45444".PadLeft(20)); // folio to cancel
_sb.Append("".PadLeft(30)); // account number (optional)
Are you sure? Try to add a separator, just to see where the substrings end
StringBuilder _sb = new StringBuilder();
_sb.Append(string.Format("{0,5}|", "MM")); // serie to cancel
_sb.Append(string.Format("{0,20}|", "45444")); // folio to cancel
_sb.Append(string.Format("{0,30}|", "")); // account number (optional
Console.WriteLine(">" + _sb.ToString() + "<");
> MM| 45444| |<
It will add up empty chars by default until the desired length matched.
Example:
"Hello".ToFixedString(10)
will produce "Hello "
.
public static class StringExtensions
{
/// <summary>
/// Extends the <code>String</code> class with this <code>ToFixedString</code> method.
/// </summary>
/// <param name="value"></param>
/// <param name="length">The prefered fixed string size</param>
/// <param name="appendChar">The <code>char</code> to append</param>
/// <returns></returns>
public static String ToFixedString(this String value, int length, char appendChar = ' ')
{
int currlen = value.Length;
int needed = length == currlen ? 0 : (length - currlen);
return needed == 0 ? value :
(needed > 0 ? value + new string(' ', needed) :
new string(new string(value.ToCharArray().Reverse().ToArray()).
Substring(needed * -1, value.Length - (needed * -1)).ToCharArray().Reverse().ToArray()));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With