Can maximum number of characters be defined in C# format strings like in C printf?

0 votes
asked Nov 5, 2010 by matti

Didn't find how to do that. What I found was more or less on the lines of this (http://blog.stevex.net/string-formatting-in-csharp/):

There really isn’t any formatting within a string, beyond it’s alignment. Alignment works for any argument being printed in a String.Format call. Sample Generates

String.Format(“->{1,10}<-”, “Hello”);  // gives "->     Hello<-" (left padded to 10)
String.Format(“->{1,-10}<-”, “Hello”); // gives "->Hello     <-" (right padded to 10)

4 Answers

0 votes
answered Nov 5, 2010 by ian-johnson

Can't you just apply length arguments using the parameter rather than the format?

String.Format("->{0}<-", toFormat.PadRight(10)); // ->Hello <-

Or write your own formatter to allow you to do this?

0 votes
answered Nov 5, 2010 by moo-juice

Why not just use Substr to limit the length of your string?

String s = "abcdefghijklmnopqrstuvwxyz";
String.Format("Character limited: {0}", s.Substr(0, 10));
0 votes
answered Nov 5, 2010 by paolo-tedesco

What you want is not "natively" supported by C# string formatting, as the String.ToString methods of the string object just return the string itself.

When you call

string.Format("{0:xxx}",someobject);

if someobject implements the IFormattable interface, the overload ToString(string format,IFormatProvider formatProvider) method gets called, with "xxx" as format parameter.

So, at most, this is not a flaw in the design of .NET string formatting, but just a lack of functionality in the string class.

If you really need this, you can use any of the suggested workarounds, or create your own class implementing IFormattable interface.

0 votes
answered Sep 15, 2017 by doug-wiley

I've written a custom formatter that implements an "L" format specifier used to set maximum width. This is useful when we need to control the size of our formatted output say when destined for a database column or Dynamics CRM field.

public class StringFormatEx : IFormatProvider, ICustomFormatter
{
    /// <summary>
    /// ICustomFormatter member
    /// </summary>
    public string Format(string format, object argument, IFormatProvider formatProvider)
    {
        #region func-y town
        Func<string, object, string> handleOtherFormats = (f, a) => 
        {
            var result = String.Empty;
            if (a is IFormattable) { result = ((IFormattable)a).ToString(f, CultureInfo.CurrentCulture); }
            else if (a != null) { result = a.ToString(); }
            return result;
        };
        #endregion

        //reality check.
        if (format == null || argument == null) { return argument as string; }

        //perform default formatting if arg is not a string.
        if (argument.GetType() != typeof(string)) { return handleOtherFormats(format, argument); }

        //get the format specifier.
        var specifier = format.Substring(0, 1).ToUpper(CultureInfo.InvariantCulture);

        //perform extended formatting based on format specifier.
        switch(specifier)
        {
            case "L": 
                return LengthFormatter(format, argument);
            default:
                return handleOtherFormats(format, argument);
        }
    }

    /// <summary>
    /// IFormatProvider member
    /// </summary>
    public object GetFormat(Type formatType)
    {
        if (formatType == typeof(ICustomFormatter))
            return this;
        else
            return null;
    }

    /// <summary>
    /// Custom length formatter.
    /// </summary>
    private string LengthFormatter(string format, object argument)
    {
        //specifier requires length number.
        if (format.Length == 1)
        {
            throw new FormatException(String.Format("The format of '{0}' is invalid; length is in the form of Ln where n is the maximum length of the resultant string.", format));
        }

        //get the length from the format string.
        int length = int.MaxValue;
        int.TryParse(format.Substring(1, format.Length - 1), out length);

        //returned the argument with length applied.
        return argument.ToString().Substring(0, length);
    }
}

Usage is

var result = String.Format(
    new StringFormatEx(),
    "{0:L4} {1:L7}",
    "Stack",
    "Overflow");

Assert.AreEqual("Stac Overflo", result);
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...