This question already has answers here:
Convert a string to an enum in C#
(29 answers)
How to get enum value by string or int
(13 answers)
Closed 1 year ago.
When I say Key, I'm referring to the "Keys" enum in Windows Forms. For instance:
I might have a string:
string key = "Q";
And I'm trying to convert it to this:
Keys.Q
How would I do this? (If even possible)
In case that the values of the string are not exactly the same as the enum you can do it with a switch-case statment.
Keys keys = key switch
{
"Q" => Keys.Q
...
};
If the values exactly the same just parse it:
public static TEnum ToEnum<TEnum>(this string strEnumValue)
{
if (!Enum.IsDefined(typeof(TEnum), strEnumValue))
{
throw new Exception("Enum can't be parsed");
}
return (TEnum)Enum.Parse(typeof(TEnum), strEnumValue);
}
Keys keys = key.ToEnum<Keys>();
Related
This question already has answers here:
Convert a string to an enum in C#
(29 answers)
Deserialize Json string to Enum C#
(2 answers)
Closed 3 years ago.
I would like to know if it is possible to convert an enum key to a string and a string to an enum key like follow:
enum AllGameSettingsLabels {
Options_Controls_Joystick_Sensitivity,
Options_Display_Screen_Resolution,
Game_Character_Name
// ...
}
IDictionary<AllGameSettingsLabels, string> settings = new Dictionary<AllGameSettingsLabels, string>();
settings[AllGameSettingsLabels.Parse(AllGameSettingsLabels.Joystick_Sensitivity.ToString() + "_Player_1")] = (0.25).ToString();
I was trying to come up with an easy way to manage all the game settings and later converting them to JSON.
This question already has answers here:
Find text in string with C#
(17 answers)
Closed 3 years ago.
How do I find out if one of my strings occurs in the other in C#?
For example, there are 2 strings:
string 1= "The red umbrella";
string 2= "red";
How would I find out if string 2 appears in string 1 or not?
you can use String Contains I guess. pretty sure there is similar question have been asked before How can I check if a string exists in another string
example :
if (stringValue.Contains(anotherStringValue))
{
// Do Something //
}
You can do it like this:
public static bool CheckString(string str1, string str2)
{
if (str1.Contains(str2))
{
return true;
}
return false;
}
Call the function:
CheckString("The red umbrella", "red") => true
This question already has answers here:
Case insensitive 'Contains(string)'
(29 answers)
C# Case insensitive string comparison [duplicate]
(3 answers)
Closed 10 months ago.
Need to have string comparison case insensitive for .net winforms application. It is not a problem when strings are compared in my code, but I need this everywhere.
For ex.: there is combobox with items populated from SQL data, where value member is uppercase string, but entity field bound to this combobox as value is allowed to have value (string) lowercase. Same for the rest elements.
You cannot change the default comparison for strings in .net. .net is a case sensitive language. It has specific methods for comparing strings using different levels of case sensitivity, but (thank goodness) no global setting.
You can use this:
string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);
Or extension method:
public static class StringExtensions
{
public static bool Contains(this string source, string value, StringComparison compareMode)
{
if (string.IsNullOrEmpty(source))
return false;
return source.IndexOf(value, compareMode) >= 0;
}
}
and you can call it like this:
bool result = "This is a try".Contains("TRY",
StringComparison.InvariantCultureIgnoreCase);
Console.WriteLine(result);
Use
if (string1.ToLower().Equals(string2.ToLower()))
{
#something
}
without the code, there is no other advice i can offer you :/
This question already has answers here:
Test if string is a guid without throwing exceptions?
(19 answers)
Closed 5 years ago.
I have string type value like "e2ddfa02610e48e983824b23ac955632". I need to add - in this code means convert in Guid.
EntityKey = "e2ddfa02610e48e983824b23ac955632";
Id = (Guid)paymentRecord.EntityKey;
Just a simple creation:
String source = "e2ddfa02610e48e983824b23ac955632";
Guid result = new Guid(source);
You could do :
Guid guid;
if (Guid.TryParse("e2ddfa02610e48e983824b23ac955632", out guid))
{
// succeed...
}
else
{
// failed...
}
Edit : Like #Silvermind said, if you know the format of the input, you can use Guid.TryParseExact with the "N" format in your case.
For parsing the string to a Guid. You can do this:
var guid= "e2ddfa02610e48e983824b23ac955632";
var result= Guid.ParseExact(guid,"N")
Or if you prefer to have it in a try parse. You can also do this:
Guid result;
if(Guid.TryParseExact(guid,"N",out result))
{
//Do something
}
The "N" is a format which indicates that the string will be format with 32 digits without "-"
Reference:
Guid.TryParseExact Method (String, String, Guid)
This question already has answers here:
Convert a string to an enum in C#
(29 answers)
Match string to enumeration?
(11 answers)
Closed 7 years ago.
I have my enum defined like this.
public enum Places : long
{
World = (long)1,
India = (long)23424848,
USA = (long)23424977
}
Now I get a string of value like 'India'. I want the corresponding value of the enumerator.
for instance if i get the string 'World'(or world - case insensitive ), I need the value 1 to be returned.
I tried this way:
long woeid = ((long)(typeof(Places)country));
this doesnot work.
Is there a simple way to do?
The method you want is Enum.Parse.
You would use it like this:
string country = "India";
Places myplace = (Places)Enum.Parse(typeof(Places), country);
long placeID = (long)Enum.Parse(typeof(Places), country);