2

Can we use the string interpolation to convert a string to upper case, just like we use it with DateTime object:

var str = $"Current date is {DateTime.Now:MM-dd-yyyy}";

Is there any string format modifier that can be passed after the : to convert string to uppercase, knowing that the culture/localization is not important?

P.S. I'm looking for a way to do that without calling string.ToUpper or ToUpperInvariant methods, I know these methods, and I can use them.

I'm looking for a "shorthand" way of doing it, just like instead of writing:

DateTime.Now.ToString("MM-dd-yyyy")

You write instead:

$"Current date is {DateTime.Now:MM-dd-yyyy}";

If there is something like this it would be awesome: {somekindOfString:U}

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • No, there is nothing. Call `ToUpperInvariant()` on it. – canton7 Feb 21 '19 at 13:07
  • @canton7 So the syntax of string interpolation doesn't support some kind of format moddifier to do that? – Mexic Mexicalian Feb 21 '19 at 13:09
  • 2
    Format modifiers control how a value is turned into a string. Strings are already strings, so they don't have them. – Jon Hanna Feb 21 '19 at 13:10
  • 1
    Indeed. `string.ToString(string format, IFormatProvider formatProvider)` simply ignores `format` -- there's nothing you can pass that will in any way affect how the string is formatted. Here: https://referencesource.microsoft.com/#mscorlib/system/FormattableString.cs,49 – canton7 Feb 21 '19 at 13:12
  • so it's not okay? ... $"Current date is {DateTime.Now.ToString("MM-dd-yyyy").ToUpper()}" – usr4217 Feb 21 '19 at 13:14
  • 1
    @usr4217 I know I can do that, I was looking for a shorthand to do it, just like instead of `DateTime.Now.ToString("MM-dd-yyyy")` you use `{DateTime.Now:MM-dd-yyyy}` – Mexic Mexicalian Feb 21 '19 at 13:16
  • 1
    What is this question? Uppercase string is not a format of `string`, it's `char` array containing different character[s] than lowercase `char`[s]. It is like if you asked can you add days to `DateTime` object with `string.Format()`. – SᴇM Feb 21 '19 at 13:16
  • 1
    I really don't understand what's the point of down-voting this question? What point are trying to highlight by down-voting my question? What sort of community-standard does this question violate? – Mexic Mexicalian Feb 21 '19 at 13:26
  • 2
    @MexicMexicalian Downvoting is not for showing that question violates some community standards, it's for to show that question does not show any research effort, it's unclear, not useful, off topic etc. – SᴇM Feb 21 '19 at 13:36

3 Answers3

3

Given the DateTime.ToString Method documentation, no. As what you want to do is manipulate string case and not DateTime formating, this makes sense.

For string interpolation quick format, you'd want that the object to be formatted implement IFormattable interface, which is not the case of String type.

MackM
  • 2,906
  • 5
  • 31
  • 45
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
0

As said before, what you need is not a DateTime method, but a string method.

didactically, i would do this:

string dt = DateTime.Now.ToString("MM-dd-yyyy");
Console.WriteLine($"current date is {dt}".ToUpperInvariant());

If you wanna know why toUpperInvariant(), and not toUpper():

it's because we will not consider the ocal culture (In C# what is the difference between ToUpper() and ToUpperInvariant()?)

  • 2
    "P.S. I'm looking for a way to do that without calling `string.ToUpper` method, I know this method, and I can use it" I think the same applies to `ToUpperInvariant`. – MakePeaceGreatAgain Feb 21 '19 at 13:35
  • Eeeehm, this was edited into the question 23min ago. Anyway your answer simply does not apply. – MakePeaceGreatAgain Feb 21 '19 at 13:42
  • I edited the question to explicitly add that part, the quote of @HimBromBeere was exactly mentioned in my question. But I figured it wasn't clear enough. – Mexic Mexicalian Feb 21 '19 at 13:45
  • 1
    Although this part clearly explains what I'm looking for: "Is there any **string format modifier that can be passed after the :** to convert string to uppercase, knowing that the culture/localization is not important?" – Mexic Mexicalian Feb 21 '19 at 13:46
0

String interpolation basically uses String.Format(...), which uses FormatHelper, which uses ... (See MS reference source) which uses the method ToString(string format, IFormatProvider formatProvider) if your object has the interface System.IFormattable implemented.

I wrote a example application to test it.

With

private class MyClass : IFormattable
{
    public string ToString(string format, IFormatProvider formatProvider)
    {
        return "I am called. format: " + format;
    }
}

You may call

MyClass mc = new MyClass();
Console.WriteLine($"myclass: {mc:MyParams}");

which produces the result

myclass: I am called. format: MyParams

Therefor, you can use string interpolation with your custom params, but you cannot use it with a System.String-object.

Chrᴉz remembers Monica
  • 1,829
  • 1
  • 10
  • 24