using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace CJavaPy.Extend
{
public static class ExtString
{
public static string NoHTML(this string Htmlstring)
{
Htmlstring = Regex.Replace(Htmlstring, @"<script[\s\S]*?</script>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<noscript[\s\S]*?</noscript>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<style[\s\S]*?</style>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<.*?>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", " ", RegexOptions.IgnoreCase);
return Htmlstring;
}
public static byte[] ToByte(this string value)
{
return System.Text.Encoding.UTF8.GetBytes(value);
}
public static string HtmlDecode(this string value)
{
return System.Net.WebUtility.HtmlDecode(value);
}
public static string HtmlEncode(this string value)
{
return System.Net.WebUtility.HtmlEncode(value);
}
public static string UrlEncode(this string value)
{
StringBuilder sb = new StringBuilder();
byte[] byStr = System.Text.Encoding.UTF8.GetBytes(value);
for (int i = 0; i < byStr.Length; i++)
{
sb.Append(@"%" + Convert.ToString(byStr[i], 16));
}
return (sb.ToString());
}
public static string ToUnicode(this string value)
{
if (string.IsNullOrEmpty(value)) return value;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < value.Length; i++)
{
builder.Append("\\u" + ((int)value[i]).ToString("x"));
}
return builder.ToString();
}
private static readonly Regex emailExpression = new Regex(@"^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.Compiled);
private static readonly Regex webUrlExpression = new Regex(@"(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?", RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.Compiled);
private static readonly Regex stripHTMLExpression = new Regex("<\\S[^><]*>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.Compiled);
public static string FormatWith(this string instance, params object[] args)
{
return string.Format(instance, args);
}
public static T ToEnum<T>(this string instance, T defaultValue) where T : struct, IComparable, IFormattable
{
T convertedValue = defaultValue;
if (!string.IsNullOrWhiteSpace(instance) && !Enum.TryParse(instance.Trim(), true, out convertedValue))
{
convertedValue = defaultValue;
}
return convertedValue;
}
public static T ToEnum<T>(this int instance, T defaultValue) where T : struct, IComparable, IFormattable
{
T convertedValue;
if (!Enum.TryParse(instance.ToString(), true, out convertedValue))
{
convertedValue = defaultValue;
}
return convertedValue;
}
public static string StripHtml(this string instance)
{
return stripHTMLExpression.Replace(instance, string.Empty);
}
public static bool IsEmail(this string instance)
{
return !string.IsNullOrWhiteSpace(instance) && emailExpression.IsMatch(instance);
}
public static bool IsWebUrl(this string instance)
{
return !string.IsNullOrWhiteSpace(instance) && webUrlExpression.IsMatch(instance);
}
public static bool AsBool(this string instance)
{
bool result = false;
bool.TryParse(instance, out result);
return result;
}
public static DateTime AsDateTime(this string instance)
{
DateTime result = DateTime.MinValue;
DateTime.TryParse(instance, out result);
return result;
}
public static Decimal AsDecimal(this string instance)
{
var result = (decimal)0.0;
Decimal.TryParse(instance, out result);
return result;
}
public static int AsInt(this string instance)
{
var result = (int)0;
int.TryParse(instance, out result);
return result;
}
public static bool IsInt(this string instance)
{
int result;
return int.TryParse(instance, out result);
}
public static bool IsDateTime(this string instance)
{
DateTime result;
return DateTime.TryParse(instance, out result);
}
public static bool IsFloat(this string instance)
{
float result;
return float.TryParse(instance, out result);
}
public static bool IsNullOrWhiteSpace(this string instance)
{
return string.IsNullOrWhiteSpace(instance);
}
public static bool IsNotNullAndWhiteSpace(this string instance)
{
return !string.IsNullOrWhiteSpace(instance);
}
public static bool IsNullOrEmpty(this string theString)
{
return string.IsNullOrEmpty(theString);
}
public static string FirstCharToLowerCase(this string instance)
{
if (instance.IsNotNullAndWhiteSpace() && instance.Length > 2 && char.IsUpper(instance[0]))
{
return char.ToLower(instance[0]) + instance.Substring(1);
}
if (instance.Length == 2)
{
return instance.ToLower();
}
return instance;
}
public static string ToFilePath(this string path)
{
return Path.Combine(path.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries));
}
public static string CombinePath(this string p, string path)
{
return $"{p.TrimEnd(Path.DirectorySeparatorChar)}{ Path.DirectorySeparatorChar}{path.ToFilePath()}";
}
}
}