Search code examples
c#stringstring-interpolation

Substring interpolation


Is it possible to do a string interpolation formatting a substring of a string?

I have been searching the Microsoft documents about string interpolation but cannot get a working sample done. reference.

Currently I have :

var description = "the quick brown fox";
var result = $"{description.Substring(0, description.Length < 10 ? description.Length : 10)} jumps..",

using string interpolation I would ideally like to use:

var description = "the quick brown fox";
var result = $"{description:10} jumps..",

editted

I would expect the output of result to be :

The quick  jumps..

Solution

  • As the question was:
    "Is it possible to do a string interpolation formatting a substring of a string?"
    In such a manner:

    var result = $"{description:10} jumps..",
    

    The answer given from @JohnSkeet and @JeroenMostert was most acurate:
    "No, it is not possible."

    There are various ways to simplify the call. thanks for @PiotrWojsa for pointing that out. however that doesnt involve the interpolation part..