Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a space in this string between words [duplicate]

Tried setting up an enum that has spaces in attributes but was very hard so figured their might be an easy way to hack this with a string format or something since their is only one enum that I need that has spaces and I know exactly what it is. Any helping wiht adding a space to this string

public class Address
{
...blah...more class datatypes here...

public AddressType Type { get; set; }  //AddressType is an enum

...blah....
}



 if (Address.Type.ToString() == "UnitedStates")
        {
           **Add space between United and States**
        }
like image 530
Jt2ouan Avatar asked Mar 05 '13 21:03

Jt2ouan


4 Answers

If your enum entries are in camel case, you can insert a whitespace before the upper letter

string res = Regex.Replace("UnitedStates", "[A-Z]", " $0").Trim();
like image 109
VladL Avatar answered Oct 16 '22 22:10

VladL


You can create your own ToString method on the enumeration using an extension method.

public static class AddressTypeExtensions
{
     public static string ToMyString(this AddressType addressType)
     {
          if (addressType == AddressType.UnitedStates)
              return "United States";

          return addressType.ToString();
     }
}
like image 36
JG in SD Avatar answered Oct 16 '22 21:10

JG in SD


This is a neat trick I found yesterday (in 2009). I wonder why I never thought of it myself. In the .net framework there is no way how to control .ToString() for enumerations. To work around that an extension method can be created as well as an attribute to decorate the different values of the enumeration. Then we can write something like this:

public enum TestEnum
{
    [EnumString("Value One")]
    Value1,

    [EnumString("Value Two")]
    Value2,

    [EnumString("Value Three")]
    Value3
}

EnumTest test = EnumTest.Value1;
Console.Write(test.ToStringEx());
The code to accomplish this is pretty simple:

[AttributeUsage(AttributeTargets.Field)]
public class EnumStringAttribute : Attribute
{
    private string enumString;

    public EnumStringAttribute(string EnumString)
    {
        enumString = EnumString;
    }

    public override string ToString()
    {
        return enumString;
    }
}

public static class ExtensionMethods
{
    public static string ToStringEx(this Enum enumeration)
    {
        Type type = enumeration.GetType();
        FieldInfo field = type.GetField(enumeration.ToString());
        var enumString =
            (from attribute in field.GetCustomAttributes(true)
             where attribute is EnumStringAttribute
             select attribute).FirstOrDefault();

        if (enumString != null)
            return enumString.ToString();

        // otherwise...
        return enumeration.ToString();
    }
}

[TestMethod()]
public void ToStringTest()
{
    Assert.AreEqual("Value One", TestEnum.Value1.ToStringEx());
    Assert.AreEqual("Value Two", TestEnum.Value2.ToStringEx());
    Assert.AreEqual("Value Three", TestEnum.Value3.ToStringEx());
}

The credit goes to this post.

like image 2
Z.D. Avatar answered Oct 16 '22 21:10

Z.D.


I have a handy Extension method for exactly this

public static class EnumExtensions
{
    public static string ToNonPascalString(this Enum enu)
    {
       return Regex.Replace(enu.ToString(), "([A-Z])", " $1").Trim();
    }

    public T EnumFromString<T>(string value) where T : struct
    {
       string noSpace = value.Replace(" ", "");
       if (Enum.GetNames(typeof(T)).Any(x => x.ToString().Equals(noSpace)))
       {
           return (T)Enum.Parse(typeof(T), noSpace);
       }
       return default(T);
    }

}

Example:

public enum Test
{
    UnitedStates,
    NewZealand
}

// from enum to string
string result = Test.UnitedStates.ToNonPascalString(); // United States

// from string to enum
Test myEnum = EnumExtensions.EnumFromString<Test>("New Zealand");  // NewZealand
like image 1
sa_ddam213 Avatar answered Oct 16 '22 21:10

sa_ddam213