====== C# Snippets ======
=== 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
public static string CombineAfterIndex(string[] InputArr, string Glue, int StartIdx) {
string OutputStr = String.Empty;
for (int i = StartIdx; i < InputArr.Length; i++) {
OutputStr += Glue + InputArr[i];
}
return OutputStr.Substring(1);
}
=== List Parameterize(string Parameters) ===
public static List Parameterize(string Parameters) {
List ReturnList = new List();
while (Parameters.Length > 0) {
if (Parameters.Substring(0, 1) == "\"") {
Parameters = Parameters.Substring(1);
if (Parameters.Contains('"')) {
ReturnList.Add(Parameters.Substring(0, Parameters.IndexOf('"')));
} else { return null; }
} else {
if (Parameters.Contains(' ')) {
ReturnList.Add(Parameters.Substring(0, Parameters.IndexOf(' ')));
} else {
ReturnList.Add(Parameters);
break;
}
}
Parameters = Parameters.Substring(ReturnList[ReturnList.Count - 1].Length);
if (Parameters.Substring(0, 1) == "\"") { Parameters = Parameters.Substring(1); }
Parameters = Parameters.TrimStart();
}
return ReturnList;
}
=== int OccurrencesInString(String Haystack, String Needle) ===
Counts the number of occurrences of Needle in Haystack.
///
/// Counts the number of occurrences of Needle in Haystack.
///
/// String being search
/// String being searched for
/// Integer representing the number of occurances of Needle in Haystack
public static int OccurrencesInString(String Haystack, String Needle) {
return Regex.Matches(Haystack, Needle).Count;
}
=== void PrintArrayList(ArrayList InputArray) ===
Prints an ArrayList object to the console.
///
/// Prints an ArrayList object to the console.
///
/// ArrayList to print.
public static void PrintArrayList(ArrayList InputArray) {
Console.WriteLine("Array Output: ");
for (int i = 0; i < InputArray.Count; i++) {
Console.WriteLine("[" + i + "] " + InputArray[i]);
}
}
=== void DumpByteArrayToFile(byte[] inputBytes, String FileName) ===
Dumps a byte[] Array to a file.
///
/// Dumps a byte[] Array to a file.
///
/// byte[] Array to export.
/// Filename to export to.
public static void DumpByteArrayToFile(byte[] inputBytes, String FileName) {
File.Delete(FileName);
FileStream MusicFileFS = new FileStream(FileName, FileMode.Create, FileAccess.ReadWrite);
if (MusicFileFS.CanWrite) {
MusicFileFS.Write(inputBytes, 0, inputBytes.Length);
}
MusicFileFS.Close();
}
=== void DumpStringToFile(String inputString, String FileName) ===
Dumps a String to a file.
///
/// Dumps a String to a file.
///
/// String to export.
/// Filename to export to.
public static void DumpStringToFile(String inputString, String FileName) {
File.Delete(FileName);
FileStream MusicFileFS = new FileStream(FileName, FileMode.Create, FileAccess.ReadWrite);
if (MusicFileFS.CanWrite) {
MusicFileFS.Write(Encoding.UTF8.GetBytes(inputString), 0, Encoding.UTF8.GetBytes(inputString).Length);
}
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.
///
/// Prints the ASCII values of every character in a character array delimited by a pipe character to the Console.
///
/// String to be split.
public static void PrintCharArrASCII(char[] inputCharArr) {
Console.Write("|");
for (int i = 0; i < inputCharArr.Length; i++) {
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.
///
/// Prints the ASCII values of every character in a string delimited by a pipe character to the Console.
///
/// String to be split.
public static void PrintStringASCII(String inputStr) {
char[] charArr = inputStr.ToCharArray();
Console.Write("|");
for (int i = 0; i < charArr.Length; i++) {
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.
///
/// Prints the ASCII values of every character in a character array delimited by a pipe character to the Console.
///
/// String to be split.
public static void PrintByteArrASCII(byte[] inputByteArr) {
char[] tempChar = Encoding.ASCII.GetChars(inputByteArr);
Console.Write("|");
for (int i = 0; i < tempChar.Length; i++) {
Console.Write(((int)tempChar[i]).ToString() + "|");
}
Console.WriteLine();
}
=== ByteArrToASCII(byte[] inputByteArr) ===
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; i++) {
TempSB.Append(((int)tempChar[i]).ToString() + "|");
}
return TempSB.ToString();
}
=== String RemoveNewLineChars(String inputStr) ===
///
/// Removes all newline characters from a string (ASCII 10/13).
///
/// String to remove characters from.
/// String lacking ASCII 10/13.
public static String RemoveNewLineChars(String inputStr) {
if (inputStr != null) {
return inputStr.Replace(Convert.ToChar(13).ToString(), "").Replace(Convert.ToChar(10).ToString(), "");
}
return null;
}
=== String StringArrToDelimitedStr(String[] inputStrArr, String Delimiter) ===
///
/// Returns a string containing the inputStrArr String Array with elemented delimited by Delimiter.
///
/// String Array to enumerate.
/// Delimiter to interleave between elements.
/// String delimited by Delimiter.
public static String StringArrToDelimitedStr(String[] inputStrArr, String Delimiter) {
String returnStr = "";
foreach (String tempStr in inputStrArr) {
returnStr += tempStr + Delimiter;
}
if (returnStr.Length > Delimiter.Length) {
returnStr = returnStr.Substring(0, (returnStr.Length - Delimiter.Length));
}
return returnStr;
}
=== String BytesToHumanReadable(long Bytes, int Accuracy) ===
///
/// Converts bytes into a human readable format rounded to an accuracy.
///
/// Number of bytes to be converted.
/// Decimal accuracy. Use 0 for no decimal point.
/// String in the format of [Number][Unit]
public static String BytesToHumanReadable(long Bytes, int Accuracy) {
if (Bytes < 1073741824) {
return Math.Round((Bytes / 1048576.0), Accuracy).ToString("0.0") + "MB";
} else if (Bytes < 1099511627776) {
return Math.Round((Bytes / 1073741824.0), Accuracy).ToString("0.0") + "GB";
} else {
return Math.Round((Bytes / 1099511627776.0), Accuracy).ToString("0.0") + "TB";
}
}
=== String MillisecondsToClockFormat(double Milliseconds) ===
///
/// Converts milliseconds to a clock-like format.
///
/// Number of milliseconds to convert
/// Returns a hh:mm:ss formated string.
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() + ":" + tempTS.Minutes.ToString("00") + ":" + tempTS.Seconds.ToString("00");
} else {
return tempTS.Minutes.ToString() + ":" + tempTS.Seconds.ToString("00");
}
}
=== String MillisecondsToHumanReadable(double Milliseconds) ===
///
/// Converts milliseconds to a human readable format.
///
/// Number of milliseconds to convert
/// Returns a #d #h #m #s string.
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() + "s";
} else if (tempTS.Hours > 0) {
return tempTS.Hours.ToString() + "h " + tempTS.Minutes.ToString() + "m " + tempTS.Seconds.ToString() + "s";
} else if (tempTS.Minutes > 0) {
return tempTS.Minutes.ToString() + "m " + tempTS.Seconds.ToString() + "s";
} else {
return tempTS.Seconds.ToString() + "s";
}
}