Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lower case Boolean.ToString() value

I am outputting the value of a boolean in my ASP.NET MVC Framework view, and would like to have a lower case true or false, rather than the default of True or False.

I understand that I could just do this:

@this.Model.MyBool.ToString().ToLower()

Alternatively, I could create an extension method. But this defeats the purpose of:

@this.Model.MyBool

I have read the post Why does Boolean.ToString output "True" and not "true", but most of the answers are pretty old.

Are there any more modern ways to accomplish this?

like image 844
rhughes Avatar asked May 26 '14 07:05

rhughes


People also ask

How do you use boolean in toString?

toString(boolean b) returns a String object representing the specified boolean. If the specified boolean is true, then the string "true" will be returned, otherwise the string "false" will be returned.

How do you convert boolean?

To convert Boolean to String in Java, use the toString() method. For this, firstly, we have declared two booleans. String str1 = new Boolean(bool1). toString(); String str2 = new Boolean(bool2).


1 Answers

If you only want this for one bool variable you should use @Mohamed 's method. Else you can create an extension method (as you already said yourself):

public static class Extensions
{
    public static string ToLowerString(this bool _bool)
    {
        return _bool.ToString().ToLower();
    }
}

Then to use it:

public static void Main()
{
    bool testBoolean = true;
    Console.WriteLine(testBoolean.ToLowerString());
}
like image 188
Jevgeni Geurtsen Avatar answered Nov 16 '22 03:11

Jevgeni Geurtsen