Files
win8phoneApp/CampusAppWP8/CampusAppWPortalLib8/Utility/DefaultStringManager.cs
Christian Fiedler 4f1ff8b0ce CountChar
2013-10-24 11:29:51 +02:00

222 lines
7.8 KiB
C#

//-----------------------------------------------------------------------
// <copyright file="DefaultStringManager.cs" company="BTU/IIT">
// The MIT License (MIT). Copyright (c) 2013 BTU/IIT.
// </copyright>
// <author>Stubbfel</author>
// <date>15.10.2013</date>
// <summary>Implements the default string manager class</summary>
//-----------------------------------------------------------------------
namespace CampusAppWPortalLib8.Utility
{
using System;
using System.Text.RegularExpressions;
using CampusAppWPortalLib8.Resources;
/// <summary> Class provides some special StringMethods. </summary>
/// <remarks> Stubbfel, 15.10.2013. </remarks>
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>
/// <remarks> Stubbfel, 15.10.2013. </remarks>
/// <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>
/// <remarks> Stubbfel, 15.10.2013. </remarks>
/// <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>
/// <remarks> Stubbfel, 15.10.2013. </remarks>
/// <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 == null)
{
return null;
}
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>
/// <remarks> Stubbfel, 15.10.2013. </remarks>
/// <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)
{
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;
}
/// <summary> Method check if the string contains only digit. </summary>
/// <remarks> Stubbfel, 15.10.2013. </remarks>
/// <param name="str"> input string. </param>
/// <returns> true if the string contains only digit, otherwise false. </returns>
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;
}
/// <summary> Method gets the placeId of the result string for an nfc string. </summary>
/// <remarks> Stubbfel, 15.10.2013. </remarks>
/// <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;
}
/// <summary>Count character.</summary>
/// <remarks>Fiedler, 27.08.2013.</remarks>
/// <param name="str">input string.</param>
/// <param name="c">The character.</param>
/// <returns>The total number of the specified character in the string.</returns>
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
}
}