Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
code_snippets:csharp_snippets [2014/06/17 17:57] – external edit 127.0.0.1 | code_snippets:csharp_snippets [2014/06/19 16:19] (current) – smark | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== C# Snippets ====== | ====== C# Snippets ====== | ||
- | Woo Content! | + | === string CombineAfterIndex(string[] InputArr, string Glue, int StartIdx) === |
+ | Combine elements of a string array with a string between them, after an index from the first array | ||
+ | <code c#> | ||
+ | public static string CombineAfterIndex(string[] InputArr, string Glue, int StartIdx) { | ||
+ | string OutputStr = String.Empty; | ||
+ | for (int i = StartIdx; i < InputArr.Length; | ||
+ | OutputStr += Glue + InputArr[i]; | ||
+ | } | ||
+ | return OutputStr.Substring(1); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === List< | ||
+ | |||
+ | <code c#> | ||
+ | public static List< | ||
+ | List< | ||
+ | while (Parameters.Length > 0) { | ||
+ | if (Parameters.Substring(0, | ||
+ | Parameters = Parameters.Substring(1); | ||
+ | if (Parameters.Contains('"' | ||
+ | ReturnList.Add(Parameters.Substring(0, | ||
+ | } else { return null; } | ||
+ | } else { | ||
+ | if (Parameters.Contains(' | ||
+ | ReturnList.Add(Parameters.Substring(0, | ||
+ | } else { | ||
+ | ReturnList.Add(Parameters); | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | Parameters = Parameters.Substring(ReturnList[ReturnList.Count - 1].Length); | ||
+ | if (Parameters.Substring(0, | ||
+ | Parameters = Parameters.TrimStart(); | ||
+ | } | ||
+ | return ReturnList; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === int OccurrencesInString(String Haystack, String Needle) === | ||
+ | Counts the number of occurrences of Needle in Haystack. | ||
+ | <code c#> | ||
+ | /// < | ||
+ | /// Counts the number of occurrences of Needle in Haystack. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// <param name=" | ||
+ | /// < | ||
+ | public static int OccurrencesInString(String Haystack, String Needle) { | ||
+ | return Regex.Matches(Haystack, | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === void PrintArrayList(ArrayList InputArray) === | ||
+ | Prints an ArrayList object to the console. | ||
+ | <code c#> | ||
+ | /// < | ||
+ | /// Prints an ArrayList object to the console. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | public static void PrintArrayList(ArrayList InputArray) { | ||
+ | Console.WriteLine(" | ||
+ | for (int i = 0; i < InputArray.Count; | ||
+ | Console.WriteLine(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === void DumpByteArrayToFile(byte[] inputBytes, String FileName) === | ||
+ | Dumps a byte[] Array to a file. | ||
+ | <code c#> | ||
+ | /// < | ||
+ | /// Dumps a byte[] Array to a file. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// <param name=" | ||
+ | public static void DumpByteArrayToFile(byte[] inputBytes, String FileName) { | ||
+ | File.Delete(FileName); | ||
+ | FileStream MusicFileFS = new FileStream(FileName, | ||
+ | if (MusicFileFS.CanWrite) { | ||
+ | MusicFileFS.Write(inputBytes, | ||
+ | } | ||
+ | MusicFileFS.Close(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === void DumpStringToFile(String inputString, | ||
+ | Dumps a String to a file. | ||
+ | <code c#> | ||
+ | /// < | ||
+ | /// Dumps a String to a file. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// <param name=" | ||
+ | public static void DumpStringToFile(String inputString, | ||
+ | File.Delete(FileName); | ||
+ | FileStream MusicFileFS = new FileStream(FileName, | ||
+ | if (MusicFileFS.CanWrite) { | ||
+ | MusicFileFS.Write(Encoding.UTF8.GetBytes(inputString), | ||
+ | } | ||
+ | MusicFileFS.Close(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === void PrintCharArrASCII(char[] inputCharArr) === | ||
+ | Prints the ASCII values of every character in a character array delimited by a pipe character to the Console. | ||
+ | <code c#> | ||
+ | /// < | ||
+ | /// Prints the ASCII values of every character in a character array delimited by a pipe character to the Console. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | public static void PrintCharArrASCII(char[] inputCharArr) { | ||
+ | Console.Write(" | ||
+ | for (int i = 0; i < inputCharArr.Length; | ||
+ | Console.Write(((int)inputCharArr[i]).ToString() + " | ||
+ | } | ||
+ | Console.WriteLine(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === void PrintStringASCII(String inputStr) === | ||
+ | Prints the ASCII values of every character in a string delimited by a pipe character to the Console. | ||
+ | <code c#> | ||
+ | /// < | ||
+ | /// Prints the ASCII values of every character in a string delimited by a pipe character to the Console. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | public static void PrintStringASCII(String inputStr) { | ||
+ | char[] charArr = inputStr.ToCharArray(); | ||
+ | Console.Write(" | ||
+ | for (int i = 0; i < charArr.Length; | ||
+ | Console.Write(((int)charArr[i]).ToString() + " | ||
+ | } | ||
+ | Console.WriteLine(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === void PrintByteArrASCII(byte[] inputByteArr) === | ||
+ | Prints the ASCII values of every character in a character array delimited by a pipe character to the Console. | ||
+ | <code c#> | ||
+ | /// < | ||
+ | /// Prints the ASCII values of every character in a character array delimited by a pipe character to the Console. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | public static void PrintByteArrASCII(byte[] inputByteArr) { | ||
+ | char[] tempChar = Encoding.ASCII.GetChars(inputByteArr); | ||
+ | Console.Write(" | ||
+ | for (int i = 0; i < tempChar.Length; | ||
+ | Console.Write(((int)tempChar[i]).ToString() + " | ||
+ | } | ||
+ | Console.WriteLine(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === ByteArrToASCII(byte[] inputByteArr) === | ||
+ | |||
+ | <code c#> | ||
+ | public static string ByteArrToASCII(byte[] inputByteArr) { | ||
+ | StringBuilder TempSB = new StringBuilder(); | ||
+ | char[] tempChar = Encoding.ASCII.GetChars(inputByteArr); | ||
+ | TempSB.Append(" | ||
+ | for (int i = 0; i < tempChar.Length; | ||
+ | TempSB.Append(((int)tempChar[i]).ToString() + " | ||
+ | } | ||
+ | return TempSB.ToString(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === String RemoveNewLineChars(String inputStr) === | ||
+ | |||
+ | <code c#> | ||
+ | /// < | ||
+ | /// Removes all newline characters from a string (ASCII 10/13). | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// < | ||
+ | public static String RemoveNewLineChars(String inputStr) { | ||
+ | if (inputStr | ||
+ | return inputStr.Replace(Convert.ToChar(13).ToString(), | ||
+ | } | ||
+ | return null; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === String StringArrToDelimitedStr(String[] inputStrArr, | ||
+ | |||
+ | <code c#> | ||
+ | /// < | ||
+ | /// Returns a string containing the inputStrArr String Array with elemented delimited by Delimiter. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// <param name=" | ||
+ | /// < | ||
+ | public static String StringArrToDelimitedStr(String[] inputStrArr, | ||
+ | String returnStr = ""; | ||
+ | foreach (String tempStr in inputStrArr) { | ||
+ | returnStr += tempStr + Delimiter; | ||
+ | } | ||
+ | if (returnStr.Length > Delimiter.Length) { | ||
+ | returnStr = returnStr.Substring(0, | ||
+ | } | ||
+ | return returnStr; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === String BytesToHumanReadable(long Bytes, int Accuracy) === | ||
+ | |||
+ | <code c#> | ||
+ | /// < | ||
+ | /// Converts bytes into a human readable format rounded to an accuracy. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// <param name=" | ||
+ | /// < | ||
+ | public static String BytesToHumanReadable(long Bytes, int Accuracy) { | ||
+ | if (Bytes < 1073741824) { | ||
+ | return Math.Round((Bytes / 1048576.0), Accuracy).ToString(" | ||
+ | } else if (Bytes < 1099511627776) { | ||
+ | return Math.Round((Bytes / 1073741824.0), | ||
+ | } else { | ||
+ | return Math.Round((Bytes / 1099511627776.0), | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === String MillisecondsToClockFormat(double Milliseconds) === | ||
+ | |||
+ | <code c#> | ||
+ | /// < | ||
+ | /// Converts milliseconds to a clock-like format. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// < | ||
+ | public static String MillisecondsToClockFormat(double Milliseconds) { | ||
+ | TimeSpan tempTS = new TimeSpan(0, 0, 0, 0, Convert.ToInt32(Milliseconds)); | ||
+ | if (tempTS.Hours > 0) { | ||
+ | return tempTS.Hours.ToString() + ":" | ||
+ | } else { | ||
+ | return tempTS.Minutes.ToString() + ":" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === String MillisecondsToHumanReadable(double Milliseconds) === | ||
+ | |||
+ | <code c#> | ||
+ | /// < | ||
+ | /// Converts milliseconds to a human readable format. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// < | ||
+ | public static String MillisecondsToHumanReadable(double Milliseconds) { | ||
+ | TimeSpan tempTS; | ||
+ | if (Milliseconds > Int32.MaxValue) { | ||
+ | tempTS = new TimeSpan(0, 0, 0, Convert.ToInt32((Milliseconds - Int32.MaxValue) / 1000), Int32.MaxValue); | ||
+ | } else { | ||
+ | tempTS = new TimeSpan(0, 0, 0, 0, Convert.ToInt32(Milliseconds)); | ||
+ | } | ||
+ | if (tempTS.Days > 0) { | ||
+ | return tempTS.Days + "d " + tempTS.Hours.ToString() + "h " + tempTS.Minutes.ToString() + "m " + tempTS.Seconds.ToString() + " | ||
+ | } else if (tempTS.Hours > 0) { | ||
+ | return tempTS.Hours.ToString() + "h " + tempTS.Minutes.ToString() + "m " + tempTS.Seconds.ToString() + " | ||
+ | } else if (tempTS.Minutes > 0) { | ||
+ | return tempTS.Minutes.ToString() + "m " + tempTS.Seconds.ToString() + " | ||
+ | } else { | ||
+ | return tempTS.Seconds.ToString() + " | ||
+ | } | ||
+ | } | ||
+ | </ |