//-----------------------------------------------------------------------
//
// The MIT License (MIT). Copyright (c) 2013 BTU/IIT.
//
// Stubbfel
// 15.10.2013
// Implements the default string manager class
//-----------------------------------------------------------------------
namespace CampusAppWPortalLib8.Utility
{
using System;
using System.Text.RegularExpressions;
using CampusAppWPortalLib8.Resources;
/// Class provides some special StringMethods.
/// Stubbfel, 15.10.2013.
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.
/// Stubbfel, 15.10.2013.
/// 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.
/// Stubbfel, 15.10.2013.
/// 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.
/// Stubbfel, 15.10.2013.
/// 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 == null)
{
return null;
}
if (shortStr.Length > maxLenght)
{
shortStr = shortStr.Substring(0, maxLenght) + suffix;
}
return shortStr;
}
/// Method gets the placeId of the result string for an qrCode string.
/// Stubbfel, 15.10.2013.
/// input qrCode string.
/// the id if it was found it in the string otherwise null.
public static string FilterPlaceIdinQRResultString(string qrcodeResult)
{
if (qrcodeResult == null)
{
return null;
}
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.
/// Stubbfel, 15.10.2013.
/// input string.
/// true if the string contains only digit, otherwise false.
public static bool IsDigitsOnly(string str)
{
if (str == null || str.Length < 1)
{
return false;
}
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.
/// Stubbfel, 15.10.2013.
/// 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;
}
/// Count character.
/// Fiedler, 27.08.2013.
/// input string.
/// The character.
/// The total number of the specified character in the string.
public static int CountChar(string str, char c)
{
int retValue = 0;
foreach(char tc in str)
{
if(tc.Equals(c) == true)
{
retValue++;
}
}
return retValue;
}
#endregion
}
}