I have an enum  Eg.  enum { APPLE, MANGO, BANANA }   and a corresponding string array  char fruits[] =  {  "apple",  "mango",  "banana" }   I need to retrieve the index of string, given I have the string. So given that the string is apple, I need to get 0 and so on. [ Enum is additionally there, might help the solution]  Is there an elegant way, to save [apple,0],[banana,1] that is short and simple, that I might use as a macro. I don't need lengthy things like a hashtable. Can Enum assist in the mapping?
32814
Here's what I am trying to do:    typedef enum { ONE, TWO, THREE } Numbers;   I am trying to write a function that would do a switch case similar to the following:   char num_str[10]; int process_numbers_str(Numbers num) {   switch(num) {     case ONE:     case TWO:     case THREE:     {       strcpy(num_str, num); //some way to get the symbolic constant name in here?     } break;     default:       return 0; //no match   return 1; }   Instead of defining at every case, is there a way to set it using the enum variable like I am trying to do above?
102259
I need to search a string in the string array. I dont want to use any for looping in it  string [] arr = {"One","Two","Three"};  string theString = "One"   I need to check whether theString variable is present in arr.
89955
If I have the following enum:  public enum ReturnValue{     Success = 0,     FailReason1 = 1,     FailReason2 = 2     //Etc... }   Can I avoid casting when I return, like this:  public static int main(string[] args){     return (int)ReturnValue.Success; }   If not, why isn't an enum value treated as an int by default?
4153
Is there some easy way to pad Strings in Java?  Seems like something that should be in some StringUtil-like API, but I can't find anything that does this.
527
From time to time I see an enum like the following:  [Flags] public enum Options  {     None    = 0,     Option1 = 1,     Option2 = 2,     Option3 = 4,     Option4 = 8 }   I don't understand what exactly the [Flags] attribute does.  Anyone have a good explanation or example they could post?
63506
This is a part of my .  I have the below logic to split a string value from a column in table tblProjects as below:  SELECT  @Docs = Documents FROM tblProjects WHERE ID = @ID  SET @Docs = @Docs + '$' SET @pos = 0 SET @len = 0  WHILE CHARINDEX('$', @Docs, @pos + 1)&gt;0 BEGIN   SET @len = CHARINDEX('$', @Docs, @pos + 1) - @pos   SET @value = SUBSTRING(@Docs, @pos, @len)    IF CHARINDEX('|', @value) &gt;0   BEGIN     SELECT  @type = SUBSTRING(@value, 1, CHARINDEX('|', @value) - 1),             @name = SUBSTRING(@value, CHARINDEX('|', @value) + 1, 200)     INSERT INTO tblDocuments VALUES(@ID, @value, @type, @name)   END       SET @pos = CHARINDEX('$', @Docs, @pos + @len) +1 END   A sample input would be   insert into tblProjects(ID, Documents)  values('P_1', 'gdgfg|jkkgk$bkgkkj|kjgkgk$vjffj|Khkgjhg$jkgkvghv|kbkghhkgk');   I need to split the Documents string and insert the values into a separate table. Values delimited by | should go into separate columns of the same row, whereas values for a particular row are delimited by $.   Is there a single query method to complete this operation, for all rows in the table, in the minimum possible time?
2136
I am looking for a way to convert a long string (from a dump), that represents hex values into a byte array.  I couldn't have phrased it better than the person that posted .  But to keep it original, I'll phrase it my own way: suppose I have a string "00A0BF" that I would like interpreted as the  byte[] {0x00,0xA0,0xBf}   what should I do?  I am a Java novice and ended up using BigInteger and watching out for leading hex zeros. But I think it is ugly and I am sure I am missing something simple.
3154
I have a string User name (sales) and I want to extract the text between the brackets, how would I do this?   I suspect sub-string but I can't work out how to read until the closing bracket, the length of text will vary.
1314
I have a char in c#:  char foo = '2';   Now I want to get the 2 into an int.  I find that Convert.ToInt32 returns the actual decimal value of the char and not the number 2.  The following will work:  int bar = Convert.ToInt32(new string(foo, 1));   int.parse only works on strings as well.    Is there no native function in C# to go from a char to int without making it a string?  I know this is trivial but it just seems odd that there's nothing native to directly make the conversion.
73501
StringTokenizer? Convert the String to a char[] and iterate over that? Something else?
85935
I've got a multidimensional associative array which includes an elements like  $data["status"] $data["response"]["url"] $data["entry"]["0"]["text"]   I've got a strings like:  $string = 'data["status"]'; $string = 'data["response"]["url"]'; $string = 'data["entry"]["0"]["text"]';   How can I convert the strings into a variable to access the proper array element? This method will need to work across any array at any of the dimensions.
101419
I've got a string in .NET which is actually a URL.  I want an easy way to get the value from a particular parameter. Normally, I'd just use Request.Params[&quot;theThingIWant&quot;], but this string isn't from the request.  I can create a new Uri item like so: Uri myUri = new Uri(TheStringUrlIWantMyValueFrom);  I can use myUri.Query to get the query string...but then I apparently have to find some regexy way of splitting it up. Am I missing something obvious, or is there no built in way to do this short of creating a regex of some kind, etc?
98355
With java.sql.ResultSet is there a way to get a column's name as a String by using the column's index? I had a look through the API doc but I can't find anything.
63970
Possible Duplicate:          I want to write a function that reverses the given string passed into it. But, I can not. If I supply the doReverse function (see code below) with a character array, my code works well.  I can't figure out why this does not work. I am able to access str[0] in doReverse, but I can't change any value of the array by using a char pointer. Any ideas?  void doReverse(char *str) {     str[0] = 'b'; }  void main(void) {     char *str = "abc";     doReverse(str);     puts(str); }   Update:  I know how to do write a reverse function by passing a character array to it:  void reverse1(char p[]) {     int i, temp, y;      for (i = 0, y = strlen(p); i &lt; y; ++i, --y) {         temp = p[y-1];         p[y-1] = p[i];         p[i] = temp;     } }   But, I want to write another version that gets a char pointer as a parameter.
26241
I have the following string array:  var sa = new string[] {"yabba","dabba","doo"};   I can convert it to "yabba, dabba, doo" it using string.Join() but what is the super-cool LINQ way of doing it?  The Join extension method seems promising but for a novice like me very confusing.
5329
I'm building a function to extend the Enum.Parse concept that  Allows a default value to be parsed in case that an Enum value is not found Is case insensitive  So I wrote the following: public static T GetEnumFromString&lt;T&gt;(string value, T defaultValue) where T : Enum {     if (string.IsNullOrEmpty(value)) return defaultValue;     foreach (T item in Enum.GetValues(typeof(T)))     {         if (item.ToString().ToLower().Equals(value.Trim().ToLower())) return item;     }     return defaultValue; }  I am getting a Error Constraint cannot be special class System.Enum. Fair enough, but is there a workaround to allow a Generic Enum, or am I going to have to mimic the Parse function and pass a type as an attribute, which forces the ugly boxing requirement to your code. EDIT All suggestions below have been greatly appreciated, thanks. Have settled on (I've left the loop to maintain case insensitivity - I am using this when parsing XML) public static class EnumUtils {     public static T ParseEnum&lt;T&gt;(string value, T defaultValue) where T : struct, IConvertible     {         if (!typeof(T).IsEnum) throw new ArgumentException(&quot;T must be an enumerated type&quot;);         if (string.IsNullOrEmpty(value)) return defaultValue;          foreach (T item in Enum.GetValues(typeof(T)))         {             if (item.ToString().ToLower().Equals(value.Trim().ToLower())) return item;         }         return defaultValue;     } }  EDIT: (16th Feb 2015) Christopher Currens has posted  below, which is well worth a look, and an upvote. I will remove this edit if the solution bubbles further up the page. EDIT 2: (13th Apr 2021) As this has now been addressed, and supported, since C# 7.3, I have changed the accepted answer, though full perusal of the top answers is worth it for academic, and historical, interest :)
872
In Java, arrays don't override toString(), so if you try to print one directly, you get the className + '@' + the hex of the  of the array, as defined by Object.toString():  int[] intArray = new int[] {1, 2, 3, 4, 5}; System.out.println(intArray);     // prints something like '[I@3343c8b3'   But usually, we'd actually want something more like [1, 2, 3, 4, 5]. What's the simplest way of doing that? Here are some example inputs and outputs:  // Array of primitives: int[] intArray = new int[] {1, 2, 3, 4, 5}; //output: [1, 2, 3, 4, 5]  // Array of object references: String[] strArray = new String[] {"John", "Mary", "Bob"}; //output: [John, Mary, Bob]