/* * Copyright (C) 2007 Eskil Bylund * * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ using System; using System.Globalization; using System.IO; using System.Text; namespace DCSharp { /// /// Helper methods for working with strings. /// public static class StringUtil { public static bool MatchesKeywords(string text, string[] keywords) { CultureInfo culture = CultureInfo.CurrentCulture; foreach (string keyword in keywords) { if (culture.CompareInfo.IndexOf(text, keyword, CompareOptions.IgnoreCase) < 0) { return false; } } return true; } public static Encoding TryGetEncoding(string encoding) { try { return Encoding.GetEncoding(encoding); } catch { } return null; } #region Path private static char[] PathSeparatorChars = new char [] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar, '\\', '/' }; public static string GetDirectoryName(string path) { int index = path.LastIndexOfAny(PathSeparatorChars); if (index > 0) { return path.Substring(0, index); } return path; } public static string GetFileName(string path) { int index = path.LastIndexOfAny(PathSeparatorChars); if (index > 0) { return path.Substring(index + 1); } return path; } #endregion #region Uri public static Uri CreateUri(string uriString) { Uri uri; if (TryCreateUri(uriString, out uri)) { return uri; } throw new UriFormatException(); } public static bool TryCreateUri(string uriString, out Uri uri) { if (!uriString.StartsWith("dchub://")) { uriString = "dchub://" + uriString; } return Uri.TryCreate(uriString, UriKind.Absolute, out uri); } #endregion } }