This question already has answers here:
C# numeric enum value as string
(8 answers)
Closed 8 years ago.
Having an enum like this:
enum Items { one, two, three, four};
How can I assign result to a string variable like
string itemType = Items.2;
or
string itemType = Items.one;
Can you please let me know if it is possible or not? Thanks
You can use Enum.GetName method.
var name = Enum.GetName(typeof (Items), Items.one);
Or:
var name = Enum.GetName(typeof (Items), 0);
Add `.ToString() to your code:
string itemType = Items.one.ToString();
Call ToString() on the enum. You should be able to do Items.one.ToString() and get the string representation of the enum.
Related
This question already has answers here:
How can I split a string with a string delimiter? [duplicate]
(7 answers)
Closed 5 years ago.
I want to separate a string and put it into an array.
Example:
id = 12,32,544,877,136,987
arraylist: [0]-->12
[1]-->32
[2]--544
[3]-->877
[4]-->136
[5]-->987
How to do that?
If your id var is a String, you can use the Split method:
id.Split(',')
Try:
string[] arraylist = id.Split(',');
Can do something like this in java :
ArrayList<String> idList = new ArrayList <String>();
String id = "12,32,544,877,136,987";
String idArr[] = id.split(",");
for(String idVal: idArr){
idList.add(idVal);
}
This question already has answers here:
How can I check if a string exists in another string
(10 answers)
Closed 5 years ago.
I'm working on a Existing Class file(.cs) which fetches a string with some data in it.
I need to check if the string contains a word. String has no blank spaces in it.
The string-
"<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>"
I need to check if the string contains 'ReleaseUserAuthPending' in it.
You can try this:
var strValue = "<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>";
if (strValue.Contains("ReleaseUserAuthPending"))
{
//Do stuff
}
Refer About String - Contains function
For your information: Contains function is case-sensitive. If you want to make this Contains function as case-insensitive. Do the following step from this link.
bool containsString = mystring.Contains("ReleaseUserAuthPending");
Try
String yourString = "<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>";
if(yourString.Contains("ReleaseUserAuthPending")){
//contains ReleaseUserAuthPending
}else{
//does not contain ReleaseUserAuthPending
}
This question already has answers here:
How can I check if a string contains a character in C#?
(8 answers)
Closed 5 years ago.
How I can check if my string has the value ".all" in. Example:
string myString = "Hello.all";
I need to check if myString has .all in order to call other method for this string, any ideas how I can do it?
you could use myString.Contains(".all")
More info here
Use IndexOf()
var s = "Hello.all";
var a = s.IndexOf(".all", StringComparison.OrdinalIgnoreCase);
If a = -1 then no occurrences found
If a = some number other than -1 you get the index (place in the string where it starts).
So a = 5 in this case
Simply call .Contains(".all") on the string object:
if (myString.Contains(".all")
{
// your code to call the other method goes here
}
There is no need for regex to do that.
Optionally, as mentioned by #ZarX in comments, you can check if the string ends with your keyword with .EndsWith(".all"), which will return true if the string ends with your keyword.
This question already has answers here:
Enum ToString with user friendly strings
(25 answers)
Closed 7 years ago.
I need my enum to return a formatted string to the view, for example:
public enum status
{
NotStarted,
InProgress,
}
return: Not Started and In Progress. How I do it? Thanks (language C#)
enums don't do that. You'd need to provide a map of enum values to display strings or you could do something like define an attribute with a display string that you can use (which requires some fiddly reflection to get the attribute for a given enum value, but has the advantage that you can define the display name right where you define the enum value).
For example, you can use a Dictionary<status,string> to map them:
var myMap = new Dictionary<status,string>()
{
{ status.NotStarted, "Not Started" },
{ status.InProgress, "In Progress" }
};
Now to get the display string for a given status value s you'd just do something like:
var s = status.NotStarted;
var displayString = myMap[s]; // "Not Started"
Of course, you'd put this in a class somewhere so it's only defined once in one place.
Another rather brittle, quick-and-dirty way to do it would be to exploit the fact that your enum names are Pascal-cased and use something like a regex to take the enum name and insert an extra space. But that's pretty hacky. So you could do something like:
var r = new Regex("([A-Z][a-z]*)([A-Z][a-z]*)");
var displayString = r.Replace(s.ToString(),"$1 $2"); // "Not Started"
But that would choke on any enum values that didn't fit the pattern of two Pascal-cased words. Of course, you could make your regex more flexible, but that's beyond the scope of the question.
Calling ToString on an emum value is the equivalent to Enum.GetName which would give you the named value i.e.
Console.WriteLine(status.NotStarted.ToString()) // NotStarted
From there, assuming the format is consistent, you could convert the string from Pascal casing to a whitespace separated string e.g.
string result = Regex.Replace(status.NotStarted, "([a-z])([A-Z])", "$1 $2");
Console.WriteLine(result); // Not Started
See example.
Enum.GetName(typeof (Status), Status.InProgress));
This question already has answers here:
Is it possible to add a string with spaces or special characters to an enum?
(2 answers)
Closed 8 years ago.
I have an enumeration as below
private enum gettype
{
Xml/adf = 1,
xml/adf and html = 2
};
but as of my knowledge I know that we could not declare special characters spaces in between.
I even tried "display" and "description" though didn't work.
So does any one know any work around.
You cannot. Enum members must be valid C# identifiers. You can decorate them with a variety of attributes, however:
using System.ComponentModel;
private enum gettype
{
[Description("Xml/adf")]
XmlAdf = 1,
[Description("Xml/adf and html")]
XmlAdfAndHtml = 2
}
Now to convert the enum value to a description string, or vice versa you'd have to use reflection, one way or other. For example:
var enumValue = gettype.XmlAdfAndHtml;
var attr = (DescriptionAttribute[])
typeof(gettype).GetField(enumValue.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), true);
var stringValue = attr[0].Description; // Xml/adf and html
Refers the below stack overflow links which may helpful to you.
Is it possible to add a string with spaces or specialcharacters to an enum
How can I use a special char in a C# enum?