| Uutiset | Koodikirjasto | Wiki | Keskustelut | FAQ | Info |
StringExtensionsTorak 24.07.08 17:51 Laajentaa string luokan toimintaa.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Globalization; using System.Text.RegularExpressions; namespace StringExtensions { public static class StringExtensions { /// <summary> /// Remove any non-numeric characters and then return the resultant string. /// Useful for parsing phone numbers. /// </summary> /// <param name="s">The text that is to be evaluated.</param> /// <returns>New string</returns> public static string RemoveNonNumeric(this string s) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.Length; i++) if (Char.IsNumber(s[i])) sb.Append(s[i]); return sb.ToString(); } /// <summary> /// Returns the substring of the first argument string that follows the first occurrence /// of the second argument string in the first argument string, or the empty /// string if the first argument string does not contain the second argument string. /// </summary> /// <param name="source">The text that is to be evaluated.</param> /// <param name="value">String to search.</param> /// <returns>New string</returns> public static string SubstringAfter(this string source, string value) { if (string.IsNullOrEmpty(value)) { return source; } CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo; int index = compareInfo.IndexOf(source, value, CompareOptions.Ordinal); if (index < 0) { //No such substring return string.Empty; } return source.Substring(index + value.Length); } /// <summary> /// Returns the substring of the first argument string that precedes the first occurrence of /// the second argument string in the first argument string, or the empty string if /// the first argument string does not contain the second argument string. /// </summary> /// <param name="source">The text that is to be evaluated.</param> /// <param name="value">String to search.</param> /// <returns>New string</returns> public static string SubstringBefore(this string source, string value) { if (string.IsNullOrEmpty(value)) { return value; } CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo; int index = compareInfo.IndexOf(source, value, CompareOptions.Ordinal); if (index < 0) { //No such substring return string.Empty; } return source.Substring(0, index); } /// <summary> /// Truncate string using dots... /// Useful for displaying long filename paths. /// </summary> /// <param name="s">String to be truncated.</param> /// <param name="maxLength">Maximum string size</param> /// <returns>New string</returns> public static string Truncate(this string s, int maxLength) { if (string.IsNullOrEmpty(s) || maxLength <= 0) return string.Empty; else if (s.Length > maxLength) return s.Substring(0, maxLength) + "..."; else return s; } /// <summary> /// Reverse string. /// </summary> /// <param name="s">String to be reversed.</param> /// <returns>Reversed string.</returns> public static string Reverse(this string s) { char[] c = s.ToCharArray(); Array.Reverse(c); return new string(c); } /// <summary> /// Test if string contains only numbers. /// </summary> /// <param name="text">The text that is to be evaluated.</param> /// <returns>True if text included only letters.</returns> public static bool IsAlpha(this string text) { foreach (char c in text.ToLower()) { if (!char.IsLetter(c)) return false; } return true; } /// <summary> /// Check is string is valid email address. /// </summary> /// <param name="email">Email to be checked.</param> /// <returns>True if email address was ok.</returns> public static bool IsValidEmailAddress(this string email) { const string REGEX_VALID_EMAIL = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|" + @"(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"; bool valid = Regex.IsMatch(email, REGEX_VALID_EMAIL); return valid; } /// <summary> /// Convert string to integer. /// </summary> /// <param name="s">String to be processed.</param> /// <returns>Integer</returns> public static int ToInteger(this string s) { int integerValue = 0; int.TryParse(s, out integerValue); return integerValue; } /// <summary> /// Compare two strings, ignore case. /// </summary> /// <param name="s1">String </param> /// <param name="s2">String 2</param> /// <returns>If equal return true.</returns> public static bool Comparei(this string s1, string s2) { CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo; int i = Compare.IndexOf(s1, s2, CompareOptions.IgnoreCase); if (i != 0 ) return true; else return false; } } } Ztane 09:09 31.7.08 Sun email-osoitevalidaattoris on hiukka kyseenalaisen kireä. Miten esmes museum-loppu?? Sentään .name-loppuisen sähkärini kelpuuttaisit, päin vastoin kuin n. 70 % viritelmistä. weicco 20:52 12.9.08 Ajattelin kirjoittaa Kuha-Wikiin näistä uusista ominaisuuksista, mutta en osaa käyttää tuota pasketta. Joku voisi tehdä alun (tai miksei vaikka kokonaan), niin minä voin sorvata lopun. |
![]() Haku
|