C# Fixed Length String
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}"...
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
5 Answers
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()));
}
}
Not sure what is best way but following should work:
string appended = string.Format("{0,5}", "MM");
appended += string.Format("{0,20}", "45444");
appended += string.Format("{0,30}", "");
public static String FixedStr(this string s, int length, char padChar = ' ')
=> String.IsNullOrEmpty(s) ? new string(padChar, length) : s.Length > length ? s.Remove(length) : s.Length < length ? s.PadRight(length) : s;
Example:
"1234567890".FixedStr(5) = "12345"
"12345".FixedStr(5) = "12345"
"12".FixedStr(5) = "12 "
"".FixedStr(5) = " "