Files
win8phoneApp/CampusAppWP8/CampusAppWPortalLib8/Utility/DefaultStringManager.cs
2013-09-19 12:34:53 +02:00

193 lines
6.5 KiB
C#

//-----------------------------------------------------------------------
// <copyright file="DefaultStringManager.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>stubbfel</author>
// <sience>06.06.2013</sience>
//----------------------------------------------------------------------
namespace CampusAppWPortalLib8.Utility
{
using System;
using System.Text.RegularExpressions;
using CampusAppWPortalLib8.Resources;
/// <summary>
/// Class provides some special StringMethods
/// </summary>
public class DefaultStringManager
{
#region Members
/// <summary>
/// Patter for Html-Tags
/// </summary>
private static readonly string HtmlTagPattern = "<.*?>";
/// <summary>The mail valid regular expression.</summary>
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
/// <summary>
/// Method removes Html-Tag of a String
/// </summary>
/// <param name="inputString">String with Html-Tags</param>
/// <returns>String without Html-Tags</returns>
public static string StripHTML(string inputString)
{
return Regex.Replace(inputString, DefaultStringManager.HtmlTagPattern, string.Empty);
}
/// <summary>
/// Method add an Newline to a string
/// </summary>
/// <param name="str">input string</param>
/// <returns>input string + newline</returns>
public static string AddNewLine(string str)
{
return str.ToString() + "\n";
}
/// <summary>Method remove(TrimEND!) an Newline to a string.</summary>
/// <remarks>Stubbfel, 12.09.2013.</remarks>
/// <param name="str">input string.</param>
/// <returns>input string - newline.</returns>
public static string RemoveNewLine(string str)
{
return str.TrimEnd('\n');
}
/// <summary>Query if 'strIn' is valid email.</summary>
/// <remarks>Stubbfel, 04.09.2013.</remarks>
/// <param name="strIn">The in.</param>
/// <returns>true if valid email, false if not.</returns>
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;
}
}
/// <summary>Creates uni telefon number.</summary>
/// <remarks>Stubbfel, 04.09.2013.</remarks>
/// <param name="input">The input.</param>
/// <returns>The new uni telefon number.</returns>
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;
}
/// <summary>
/// Method create from a long string a short string
/// </summary>
/// <param name="longStr">the long input String</param>
/// <param name="maxLenght">max length of the short string</param>
/// <param name="suffix">(optional) suffix which added to the short string, when long string has to be cut</param>
/// <returns>the shorted string</returns>
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;
}
/// <summary>
/// Method gets the placeId of the result string for an qrCode string
/// </summary>
/// <param name="qrcodeResult">input qrCode string</param>
/// <returns>the id if it was found it in the string otherwise null</returns>
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;
}
/// <summary>
/// Method check if the string contains only digit
/// </summary>
/// <param name="str">input string</param>
/// <returns>true if the string contains only digit, otherwise false</returns>
public static bool IsDigitsOnly(string str)
{
foreach (char c in str)
{
if (c < '0' || c > '9')
{
return false;
}
}
return true;
}
/// <summary>
/// Method gets the placeId of the result string for an nfc string
/// </summary>
/// <param name="nfcResult">input nfc string</param>
/// <returns>the id if it was found it in the string otherwise null</returns>
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
}
}