/* * * Erevlydeux.Utilities - common and misc. utilities * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */ using System; using System.Collections; namespace Erevlydeux.Utilities { /// /// This class deals with string arrays, ArrayLists of just strings, or just regular ArrayLists. /// It was born out of a need to deal with string arrays, and due to their ease of use, ArrayLists. /// Some things are new, some things are neat wrappers for the ugly .NET equivalent, and some things /// build on their .NET counterpart. /// public sealed class ArrayUtils { /// /// Convert an ArrayList to a string array /// /// The ArrayList to convert /// A string array of the ArrayList [CLSCompliant(true)] public static string[] ToStringArray(ArrayList inputArray) { return (string[])inputArray.ToArray(typeof(string)); } /// /// Convert a string array to an ArrayList /// /// The string array to convert /// An ArrayList of the string array [CLSCompliant(true)] public static ArrayList ToArrayList(string[] inputArray) { ArrayList tempArray = new ArrayList(); foreach (string tempSz in inputArray) { tempArray.Add(tempSz); } return tempArray; } /// /// Combines the strings of a string array into one delimited string /// /// The string array to combine /// The delimited /// A string of the delimited strings [CLSCompliant(true)] public static string ToDelimitedString(string[] inputArray, string delimiter) { string returnSz = ""; if (inputArray.Length > 1) returnSz = String.Join(delimiter, inputArray); else returnSz = inputArray[0]; return returnSz; } /// /// Combines the strings of an ArrayList into one delimited string /// /// The ArrayList to combine /// The delimited /// A string of the delimited strings [CLSCompliant(true)] public static string ToDelimitedString(ArrayList inputArray, string delimiter) { string returnSz = ""; if (inputArray.Count > 1) returnSz = String.Join(delimiter, ToStringArray(inputArray)); else returnSz = (string)inputArray[0]; return returnSz; } /// /// Combines the strings of a string array into a string, which resemble a list /// /// The string array to combine /// A string which resembles a list, using commas, and follows the rules of English grammar [CLSCompliant(true)] public static string GetReadableList(string[] szArray) { if (szArray.Length == 0) return "none"; if (szArray.Length == 1) return szArray[0]; string list = String.Join(";", szArray); list = list.Replace(";", ", "); int lastSplitPos = list.LastIndexOf(", "); list = list.Insert(lastSplitPos + 2, "and "); return list; } /// /// Combines the strings of an ArrayList into a string, which resemble a list /// /// The ArrayList to combine /// A string which resembles a list, using commas, and follows the rules of English grammar [CLSCompliant(true)] public static string GetReadableList(ArrayList inputArray) { if (inputArray.Count == 0) return "none"; if (inputArray.Count == 1) return (string)inputArray[0]; string[] szArray = (string[])inputArray.ToArray(typeof(string)); string list = String.Join(";", szArray); list = list.Replace(";", ", "); int lastSplitPos = list.LastIndexOf(", "); list = list.Insert(lastSplitPos + 2, "and "); return list; } /// /// Removes all the entries from one ArrayList in another ArrayList /// /// The ArrayList to remove from /// The ArrayList to remove /// An ArrayList that does not contain the elements of arrayToRemoveFrom [CLSCompliant(true)] public static ArrayList RemoveEntries(ArrayList arrayToRemove, ArrayList arrayToRemoveFrom) { ArrayList returnArray = arrayToRemoveFrom; foreach (object entry in arrayToRemove) { if (returnArray.Contains(entry)) { returnArray.Remove(entry); } } return returnArray; } /// /// Applies a string to existing strings in an ArrayList, at a certain position in the string /// /// The ArrayList to apply the string to /// The position to apply the string, if -1 the string is applied at the end /// The string to pply /// An ArrayList with the new strings [CLSCompliant(true)] public static void AppendToElements(ArrayList inputArray, int position, string appendage) { string tempString = ""; ArrayList returnList = new ArrayList(); foreach (string element in inputArray) { tempString = (position == -1 ? element.Insert(element.Length, appendage) : element.Insert(position, appendage)); returnList.Add(tempString); } inputArray = returnList; } /// /// Applies a string to existing strings in an ArrayList, at a certain position in the string /// /// The string array to apply the string to /// The position to apply the string, if -1 the string is applied at the end /// The string to pply /// An ArrayList with the new strings [CLSCompliant(true)] public static string[] AppendToElements(string[] szArray, int position, string appendage) { int position = -1; string tempString = ""; string[] returnList = new string[szArray.Length]; foreach (string element in szArray) { tempString = (position == -1 ? element.Insert(element.Length, appendage) : element.Insert(position, appendage)); returnList[++i] = tempString; } return returnList; } /// /// Safely splits a string without erroring if the delimiter is not present /// /// The string to split /// The character to split on /// A string array of the split string [CLSCompliant(true)] public static string[] Split(string inputSz, char delimiter) { if (String.IsNullOrEmpty(inputSz)) return new string[] { }; if (inputSz.IndexOf(delimiter) == -1) return new string[] { inputSz }; return inputSz.Split(delimiter); } /// /// Pops an item off the end of a string array /// /// The array to push the item from /// The new string array, without the previous last item [CLSCompliant(true)] public static string[] Pop(string[] szArray) { string[] newSzArray = new string[szArray.Length - 1]; for (int position = 0; position < szArray.Length - 1; position++) { newSzArray[position] = szArray[position]; } return newSzArray; } /// /// Pops an item off the end of an ArrayList /// /// The array to push the item from /// The new ArrayList, without the previous last item [CLSCompliant(true)] public static void Pop(ArrayList inputArray) { ArrayList newArray = new ArrayList(); if (inputArray.Count > 0) { for (int position = 0; position < inputArray.Count - 1; position++) { newArray.Add(inputArray[position]); } } inputArray = newArray; } /// /// Pops an item off the front of a string array /// /// The array to push the item from /// The new string array, without the previous first item [CLSCompliant(true)] public static string[] ReversePop(string[] inputArray) { string[] newArray = new string[inputArray.Length - 1]; for (int position = 0; position < inputArray.Length - 1; position++) { newArray[position] = inputArray[position + 1]; } return newArray; } /// /// Pops an item off the front of an ArrayList /// /// The array to push the item from /// The new ArrayList, without the previous first item [CLSCompliant(true)] public static void ReversePop(ArrayList inputArray) { ArrayList newArray = new ArrayList(); if (inputArray.Count > 0) { for (int position = 0; position < inputArray.Count - 1; position++) { newArray.Add(inputArray[position + 1]); } } inputArray = newArray; } /// /// Pops an item off the end of a string array /// [CLSCompliant(true)] public static string[] Push(string[] szArray, string inputSz) { string[] newSzArray = new string[szArray.Length - 1]; for (int position = 0; position < szArray.Length - 1; position++) { newSzArray[position] = szArray[position]; } return newSzArray; } /// /// Pushes a string onto the end of an ArrayList /// [CLSCompliant(true)] public static void Push(ArrayList inputArray, string inputSz) { inputArray.Insert(inputArray.Count, inputSz); } /// /// Pushes an object onto the end of an ArrayList /// [CLSCompliant(true)] public static void Push(ArrayList inputArray, object inputObj) { inputArray.Insert(inputArray.Count, inputObj); } /// /// Pushes a string onto the front of a string array /// [CLSCompliant(true)] public static string[] ReversePush(string[] inputArray, string inputSz) { string[] newArray = new string[inputArray.Length - 1]; for (int position = 0; position < inputArray.Length - 1; position++) { newArray[position] = inputArray[position + 1]; } return newArray; } /// /// Pushes a string onto the front of an ArrayList /// [CLSCompliant(true)] public static void ReversePush(ArrayList inputArray, string inputSz) { ArrayList newArray = new ArrayList(); if (inputArray.Count > 0) { newArray.Add(inputSz); newArray.AddRange(inputArray); } inputArray = newArray; } /// /// Pushes an item onto the front of an ArrayList /// [CLSCompliant(true)] public static void ReversePush(ArrayList inputArray, object inputObj) { ArrayList newArray = new ArrayList(); if (inputArray.Count > 0) { newArray.Add(inputObj); newArray.AddRange(inputArray); } inputArray = newArray; } } }