# Converting string to enum

**URL:** <https://forums.unrealengine.com/t/converting-string-to-enum/55936>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [February 19, 2016, 9:40pm UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936 "2016-02-19T21:40:57Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_b9eba5b71](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b9eba5b71](https://forums.unrealengine.com/u/anonymous_user_b9eba5b71)\
**Post date:** [February 19, 2016, 9:40pm UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/1 "2016-02-19T21:40:57Z")

</div>

This may seem like a weird question, but i have those strings that have been created with EnumToString node to populate a ComboBox, now i would like to get those ComboBox strings back to their respective enum so i can use them, how can i do that ?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_3a8723ae](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_3a8723ae](https://forums.unrealengine.com/u/anonymous_user_3a8723ae)\
**Post date:** [February 20, 2016, 8:04pm UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/2 "2016-02-20T20:04:03Z")

</div>

Well,

you could do a switch on string or many if-blocks to get your enum based on the string.

Another idea could be to have a struct with enum and string as its member. Now instead of filling your combobox with strings, you fill it with a custom widget (f.e. ComboBox Item), this widget will have your struct as a member is is now able te return the string OR the enum.

---

<div class="post-metadata">

**Author:** ![Wobbleyheadedbob](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wobbleyheadedbob](https://forums.unrealengine.com/u/Wobbleyheadedbob)\
**Post date:** [June 19, 2018, 6:34pm UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/3 "2018-06-19T18:34:40Z")

</div>

This worked for me:

 ![Enum-from-ComboBox.png](https://d3kjluh73b9h9o.cloudfront.net/original/3X/0/7/07cc4880fec3b74c16c36f62b19e8d158b80a1d5.png)

---

<div class="post-metadata">

**Author:** ![Shmoopy1701](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/shmoopy1701/32/1194536_2.png) [@Shmoopy1701](https://forums.unrealengine.com/u/Shmoopy1701)\
**Post date:** [June 19, 2018, 7:30pm UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/4 "2018-06-19T19:30:01Z")

</div>

Simple way - in your enum .h file put two functions:

- GetStringForEnum(enum e) - switch on enum and return string
- GetEnumForString(fstring s) - if/else on fstring and return enum

That way every file that includes the enum also includes the conversion functions.

---

<div class="post-metadata">

**Author:** ![TheAxcell](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheAxcell](https://forums.unrealengine.com/u/TheAxcell)\
**Post date:** [June 19, 2018, 10:57pm UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/5 "2018-06-19T22:57:54Z")

</div>

In regard to the topmost answer, using a bunch of if’s or a switch in order to convert a string to a number and then back to an enum is a really bad and unoptimized way of doing it.

```auto

enum EMyEnum
{
    // ...
};

FString fsComboBoxValue = TEXT("1"); // Your combobox value

int32 iEnumValue = FCString::Atoi(*fsComboBoxValue);

EMyEnum myEnum = EMyEnum((uint8)iEnumValue);

```

If FCString is undefined you might need to include: Runtime/Core/Public/Misc/CString.h

---

<div class="post-metadata">

**Author:** ![SimplestWay](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SimplestWay](https://forums.unrealengine.com/u/SimplestWay)\
**Post date:** [August 31, 2018, 4:00am UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/6 "2018-08-31T04:00:14Z")

</div>

Thanks to TheAxcell’s code above, I can improve the code to get enum value by name.  
At first, create your own enum like this:  
-------------------code start-------------------------------  
UENUM(BlueprintType)  
enum class EAnimState : uint8  
{  
Default,  
Idle,  
Jump,  
};  
-------------------code end-----------------------------------

Then, parse string to your enum type anywhere you want:

-------------------code start-------------------------------  
const UEnum\* AnimStateEnum = FindObject\<UEnum\>(ANY\_PACKAGE, TEXT(“EAnimState”), true);  
if (AnimStateEnum) {  
int32 Index = AnimStateEnum-\>GetIndexByName(AnimStateName);  
EAnimState AnimState = EAnimState((uint8)Index);  
return AnimState;  
}  
-------------------code end----------------------------------  
Note:

1. If Index is out of range, first element in EAnimState will be returned, so, keep your default enum value in the first place.
2. This works in Unreal Engine version 4.17.X.

---

<div class="post-metadata">

**Author:** ![CptLongArm](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CptLongArm](https://forums.unrealengine.com/u/CptLongArm)\
**Post date:** [October 26, 2018, 7:54am UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/7 "2018-10-26T07:54:19Z")

</div>

How I tackle this is with BP:  
I make a map -\> String - Enum.  
If the string corresponds in the map we get the enum as return.

In my project I have objects that can reside in an area. But also in multiple areas. So i make a string like house/garage/garden.  
I split the string by ‘/’. Each return is checked in the map which gives me the corresponding enum. The ‘Right S’ is the new string to search for. If null, we exit otherwise we loop.

---

<div class="post-metadata">

**Author:** ![joel.m3c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@joel.m3c](https://forums.unrealengine.com/u/joel.m3c)\
**Post date:** [May 15, 2019, 5:49pm UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/8 "2019-05-15T17:49:04Z")

</div>

I had exactly the same problem, exactly in the same context.

Thanks to @Wobbleyheadedbob for hinting _Find Option Index_. [ATTACH=JSON]{“alt”:“ComboBox: From string to Enum”,“data-align”:“none”,“data-size”:“full”,“data-tempid”:“temp\_164599\_1557942614516\_64”,“title”:“checkpoints.PNG”,“caption”:“ComboBox: From string to Enum”}[/ATTACH]

---

<div class="post-metadata">

**Author:** ![Graphenes](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Graphenes](https://forums.unrealengine.com/u/Graphenes)\
**Post date:** [May 22, 2019, 11:03pm UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/9 "2019-05-22T23:03:01Z")

</div>

At startup populate a String/Enum Map. Query the map with ‘Find’ using your string as input. It will return the enum. E\_Settings is just the name of my enum, substitute your own. The node leading into the ADD which is converting enum to string is the ‘Enum to String’ node. Be sure NOT to use the ‘ToString’ node.

---

<div class="post-metadata">

**Author:** ![Gimli\_Gloin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Gimli\_Gloin](https://forums.unrealengine.com/u/Gimli_Gloin)\
**Post date:** [December 13, 2022, 11:21pm UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/10 "2022-12-13T23:21:21Z")

</div>

thank you great stuff  
good Graphenes but still bad Graphene

---

<div class="post-metadata">

**Author:** ![TheOneDarkling](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheOneDarkling](https://forums.unrealengine.com/u/TheOneDarkling)\
**Post date:** [August 9, 2023, 8:18am UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/11 "2023-08-09T08:18:02Z")

</div>

Best solution for scalability. You can replace GetIndexByName with GetIndexByNameString if you have a FString.

---

<div class="post-metadata">

**Author:** ![DuneGames](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/dunegames/32/538495_2.png) [@DuneGames](https://forums.unrealengine.com/u/DuneGames)\
**Post date:** [October 17, 2023, 2:44am UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/12 "2023-10-17T02:44:30Z")

</div>

Blueprint maybe a bit brute force, but this should work for you. Unfortunately, there’s no break option for ForEach over enums.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/d/e/4de03a1987653e5a282b7aa02286d4f235f7498a.png)

---

<div class="post-metadata">

**Author:** ![UrWaifu420](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/urwaifu420/32/88711_2.png) [@UrWaifu420](https://forums.unrealengine.com/u/UrWaifu420)\
**Post date:** [February 6, 2024, 12:43am UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/13 "2024-02-06T00:43:37Z")

</div>

This is what I have been using, hope it helps someone 😁

```auto
// .h
UFUNCTION(BlueprintPure, Category = "BPFunctions")
static ERegionInfo GetRegionInfoFromString(FString RegionString);

// .cpp
ERegionInfo PROJ_BlueprintFunctions::GetRegionInfoFromString(FString RegionString)
{
	const FString ERegionInfoString{ TEXT("ERegionInfo") };
	const UEnum* RegionInfoEnum{ FindObject<UEnum>(ANY_PACKAGE, *ERegionInfoString, true) };
	
	if (RegionInfoEnum) 
	{
		int32 Index{ RegionInfoEnum->GetIndexByNameString(RegionString) };
		const ERegionInfo RegionInfoCheck{ ERegionInfo((uint8)Index) };
		
		// Check if the enum is valid
		if (UEnum::GetValueAsString(RegionInfoCheck) != TEXT("None"))
		{
			return RegionInfoCheck;
		}
	}

	// Fallback if nothing is found
	const ERegionInfo RegionInfo{ ERegionInfo::RE_NoSelection };
	return RegionInfo;
}

```

---

<div class="post-metadata">

**Author:** ![UrWaifu420](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/urwaifu420/32/88711_2.png) [@UrWaifu420](https://forums.unrealengine.com/u/UrWaifu420)\
**Post date:** [February 6, 2024, 6:03am UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/14 "2024-02-06T06:03:41Z")

</div>

Also if this helps anyone, if you need to convert the enum to its non-display name as a string.

```auto
FString PROJ_BlueprintFunctions::GetRegionInfoNonDisplayString(ERegionInfo RegionInfo)
{
	FString ERegionInfoString{ UEnum::GetValueAsString(RegionInfo) };
	ERegionInfoString.RemoveFromStart(TEXT("ERegionInfo::"));

	return ERegionInfoString;
}

```

---

<div class="post-metadata">

**Author:** ![TheFluoFrench](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheFluoFrench](https://forums.unrealengine.com/u/TheFluoFrench)\
**Post date:** [April 7, 2024, 2:53pm UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/15 "2024-04-07T14:53:34Z")

</div>

I’m on the same process for now … i don’t know if it’s optimal but it works.  
Thanks for your post !

---

<div class="post-metadata">

**Author:** ![spacebares](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@spacebares](https://forums.unrealengine.com/u/spacebares)\
**Post date:** [May 16, 2024, 1:26am UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/16 "2024-05-16T01:26:44Z")

</div>

thanks, this is clean and still works in ue 5.3

---

<div class="post-metadata">

**Author:** ![klobodnf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@klobodnf](https://forums.unrealengine.com/u/klobodnf)\
**Post date:** [August 15, 2024, 2:12pm UTC](https://forums.unrealengine.com/t/converting-string-to-enum/55936/17 "2024-08-15T14:12:03Z")

</div>

```
if (const UEnum* ECultureTypePtr = StaticEnum<ECultureType>())
{
    const int32 Index = ECultureTypePtr->GetIndexByNameString(CurrentCultureName);
    const ECultureType GetCultureType = static_cast<ECultureType>(Index);

```

}
