Home > front end >  GetType Name, substrings and splits
GetType Name, substrings and splits

Time:01-10

I am trying to return the name of a GradePointState subtype such as (SuspendedState, ProbationState, etc...) of the current instance but I only want text prior to the word "State" to be returned. Instead of "ProbationState###", I want "Probation".

I have this section of code so far:

public abstract class GradePointState
{
    public string Description 
    {
        get
        {
            return GetType().Name;
        }
    }
}

And I am talking about these classes:

public class HonoursState
{
    private static HonoursState honourState;
}

public class ProbationState
{
    private static ProbationState probationState;
}

public class SuspendedState
{
    private static SuspendedState suspendedState;
}

I know I need to use the GetType().Name in this case and I'm assuming I need to use a substring or split along with it.

I'm looking for some advice on how to accomplish this.

CodePudding user response:

Simplest solution is just remove "State" from string.

public abstract class GradePointState
{
    public string Description 
    {
        get
        {
            return GetType().Name.Replace("State","");
        }
    }
}
  •  Tags:  
  • Related