Removing underscores in an enum list when displaying on the UI, using C#

Hey guys, so my problem is basically in the description. I’m working my way through the hack and slash tutorials and learning C# by doing it and then making changes till I get my desired results. However, I’ve run into a bit of a conundrum. He doesn’t show a way to remove the underscores from his UI and it just looks rather unclean with them in there. I know an enum list must have underscores if you want to make it two words, but I also know there is a way to display the enum list without displaying the underscores. My problem lies in the fact that I could not find an example of someone explaining how to do this. I’m also sure there’s a better way to display this material rather then through enums, so if you so desire on enlightening me I’m more then happy to listen. Thanks for any and all help, my code is listed below:

my enum list:

public enum SkillName {
	Melee_Offense,
	Melee_Defense,
	Ranged_Offense,
	Ranged_Defense,
	Magic_Offense,
	Magic_Defense

code calling the list for display:

private void DisplaySkills(){

		for (int cnt = 0; cnt < Enum.GetValues (typeof(SkillName)).Length; cnt++) {
			GUI.Label (new Rect (OFFSET * 2 + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH + BUTTON_WIDTH * 2
 + OFFSET * 2, 35 + (cnt * LINE_HEIGHT),  STAT_LABEL_WIDTH, LINE_HEIGHT), ((SkillName)cnt).ToString ());

once again, thank you.

http://msdn.microsoft.com/en-us/library/system.string.replace(v=vs.90).aspx

Best bet is probably the ToString() method. Borrowing an example…

EmloyeeRole role = EmloyeeRole.Day_Manager; 
string roleString = role.ToString();

Then you can loop through the string contents to replace whatever characters you like, or use the string Replace() method:

roleString.Replace("_", " ");

I think Replace() is part of the System namespace, so you may need to have a using System; declaration at the top of the script with the other using statements. Try without it and see if it compiles.

Anyone know of a better way to do this? Happy coding, and welcome to the forum!

I think I understand the concept, but I’m still not quite getting it to work. Is it because I call the whole enum using ((SkillName)cnt).ToString()); instead of creating a string for each and calling just that string and then replacing the underscores?

The int-to-enum cast might be messing you up. Not super sure why, this is unfamiliar. Run a test where all you do is convert one enum instance to a string and Debug.Log() it to see if you’re getting the right output.

ok, thanks sunny. There’s a strong chance I’m just screwing something up in my coding, since I’m fairly new, but I feel like I have a reasonably good grasp on it and I just can’t seem to figure out how to change it. I just may end up making it a static list, although I’d like to learn how to change this. I even tried using descriptions and calling those but I couldn’t figure that out either… Struggle bus on this one for sure…

figured it out, I was screwing something up although not sure what… Thanks for the help Sunny!