//----------------------------------------------------------------------- // // Company copyright tag. // // stubbfel // 06.06.2013 //---------------------------------------------------------------------- namespace CampusAppWPortalLib8.Utility { using System; using System.Text.RegularExpressions; using CampusAppWPortalLib8.Resources; /// /// Class provides some special StringMethods /// public class DefaultStringManager { #region Members /// /// Patter for Html-Tags /// private static readonly string HtmlTagPattern = "<.*?>"; /// The mail valid regular expression. private static readonly string EMailValidRegex = @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$"; #endregion #region Methods /// /// Method removes Html-Tag of a String /// /// String with Html-Tags /// String without Html-Tags public static string StripHTML(string inputString) { return Regex.Replace(inputString, DefaultStringManager.HtmlTagPattern, string.Empty); } /// /// Method add an Newline to a string /// /// input string /// input string + newline public static string AddNewLine(string str) { return str.ToString() + "\n"; } /// Method remove(TrimEND!) an Newline to a string. /// Stubbfel, 12.09.2013. /// input string. /// input string - newline. public static string RemoveNewLine(string str) { return str.TrimEnd('\n'); } /// Query if 'strIn' is valid email. /// Stubbfel, 04.09.2013. /// The in. /// true if valid email, false if not. public static bool IsValidEmail(string strIn) { // Return true if strIn is in valid e-mail format. try { return Regex.IsMatch( strIn, DefaultStringManager.EMailValidRegex, RegexOptions.IgnoreCase); } catch (Exception) { return false; } } /// Creates uni telefon number. /// Stubbfel, 04.09.2013. /// The input. /// The new uni telefon number. public static string CreateUniTelefonNumber(string input) { string result = null; if (input.Length < 5) { result = Constants.UniCBTelPrefix + input.TrimStart('0'); } else { result = input; } Regex regexObj = new Regex(@"[^\d]"); result = regexObj.Replace(result.TrimStart('0'), string.Empty); result = Constants.DeTelPrefix + result; return result; } /// /// Method create from a long string a short string /// /// the long input String /// max length of the short string /// (optional) suffix which added to the short string, when long string has to be cut /// the shorted string public static string ToShortString(string longStr, int maxLenght, string suffix = "") { string shortStr = longStr; if (shortStr.Length > maxLenght) { shortStr = shortStr.Substring(0, maxLenght) + suffix; } return shortStr; } /// /// Method gets the placeId of the result string for an qrCode string /// /// input qrCode string /// the id if it was found it in the string otherwise null public static string FilterPlaceIdinQRResultString(string qrcodeResult) { string[] lines = qrcodeResult.Split('\n'); string tmpLineTrim; foreach (string line in lines) { tmpLineTrim = line.Trim(); if (DefaultStringManager.IsDigitsOnly(tmpLineTrim)) { return tmpLineTrim; } } return null; } /// /// Method check if the string contains only digit /// /// input string /// true if the string contains only digit, otherwise false public static bool IsDigitsOnly(string str) { foreach (char c in str) { if (c < '0' || c > '9') { return false; } } return true; } /// /// Method gets the placeId of the result string for an nfc string /// /// input nfc string /// the id if it was found it in the string otherwise null public static string FilterPlaceIdinNFCResultString(string nfcResult) { string nfcResultTrim = nfcResult.Trim('{'); nfcResultTrim = nfcResultTrim.Trim('}'); string[] items = nfcResultTrim.Split(','); string[] tmpStringPair; foreach (string item in items) { tmpStringPair = item.Trim().Split(':'); if (tmpStringPair.Length == 2) { string pairKey = tmpStringPair[0].Trim('\"').Trim(); if (pairKey.Equals("pid")) { return tmpStringPair[1].Trim('\"').Trim(); } } } return null; } #endregion } }