// NAnt - A .NET build tool // Copyright (C) 2001-2004 Gerry Shaw // // 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 // // Gert Driesen (gert.driesen@ardatis.com) using System; using System.Collections.Specialized; namespace NAnt.Core.Util { /// /// Groups a set of useful manipulation and validation /// methods. /// public sealed class StringUtils { #region Private Instance Constructors /// /// Initializes a new instance of the class. /// /// /// Prevents instantiation of the class. /// private StringUtils() { } #endregion Private Instance Constructors #region Public Static Methods /// /// Determines whether the last character of the given /// matches the specified character. /// /// The string. /// The character. /// /// if the last character of /// matches ; otherwise, . /// /// is . public static bool EndsWith(string value, char c) { if (value == null) { throw new ArgumentNullException("value"); } int stringLength = value.Length; if ((stringLength != 0) && (value[stringLength - 1] == c)) { return true; } return false; } /// /// Indicates whether or not the specified is /// or an string. /// /// The value to check. /// /// if is /// or an empty string (""); otherwise, . /// public static bool IsNullOrEmpty(string value) { return (value == null || value.Trim().Length == 0); } /// /// Converts an empty string ("") to . /// /// The value to convert. /// /// if is an empty /// string ("") or ; otherwise, . /// public static string ConvertEmptyToNull(string value) { if (!IsNullOrEmpty(value)) { return value; } return null; } /// /// Converts to an empty string. /// /// The value to convert. /// /// An empty string if is ; /// otherwise, . /// public static string ConvertNullToEmpty(string value) { if (value == null) { return string.Empty; } return value; } /// /// Concatenates a specified separator between each /// element of a specified , yielding a /// single concatenated string. /// /// A . /// A . /// /// A consisting of the elements of /// interspersed with the separator string. /// /// /// /// For example if is ", " and the elements /// of are "apple", "orange", "grape", and "pear", /// returns "apple, orange, /// grape, pear". /// /// /// If is , an empty /// string () is used instead. /// /// public static string Join(string separator, StringCollection value) { if (value == null) { throw new ArgumentNullException("value"); } if (separator == null) { separator = string.Empty; } // create with size equal to number of elements in collection string[] elements = new string[value.Count]; // copy elements in collection to array value.CopyTo(elements, 0); // concatenate specified separator between each elements return string.Join(separator, elements); } /// /// Creates a shallow copy of the specified . /// /// The that should be copied. /// /// A shallow copy of the specified . /// public static StringCollection Clone(StringCollection stringCollection) { string[] strings = new string[stringCollection.Count]; stringCollection.CopyTo(strings, 0); StringCollection clone = new StringCollection(); clone.AddRange(strings); return clone; } #endregion Public Static Methods } }