/*
* 作者:吴隆健
* 日期: 2019-04-07
* 时间: 20:05
*
*/
using System;
using System.Text.RegularExpressions;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Data;
namespace MrWu.Basic
{
///
/// 常用的一些方法_变量
///
public static class BaseFun
{
private static readonly Random _random = new Random();
///
/// 随机数
///
public static Random random {
get {
return _random;
}
}
///
/// 检查用户名_检查字符串只包含 字母数字 下划线
///
/// 用户名
/// 最小多长
/// 最大多长
/// true 表示通过 false 表示不通过
public static bool CheckUserName(string username, int minlen, int maxlen)
{
//0-9 Ascii 48-57
//a-z Ascii 97-122
//A-Z Ascii 65-90
//_ Ascii 95
if (username == null)
return false;
//[]里面表示匹配a-z A-Z 0-9 _
//如果前面在^表示匹配上述范围之外的
//{n,m}表示匹配到n-m次
//return Regex.IsMatch(username, @"^[a-zA-Z0-9]{3,6}");
int len = username.Length;
if (len < minlen || len > maxlen)
return false;
for (int i = 0; i < len; i++)
{
if (username[i] >= 97 && username[i] <= 122)
continue;
if (username[i] >= 65 && username[i] <= 90)
continue;
if (username[i] >= 48 && username[i] <= 57)
continue;
if (username[i] == 95)
continue;
return false;
}
return true;
}
public static bool CheckNickName(string nickname, int minlen, int maxlen)
{
if (nickname == null)
return false;
int len = nickname.Length;
if (len < minlen || len > maxlen)
return false;
byte[] bts = Encoding.UTF8.GetBytes(nickname);
for (int i = 0; i < len; i++)
{
if (bts[i] > 127 || (bts[i] >= 48 && bts[i] <= 57) || (bts[i] >= 65 && bts[i] <= 90) ||
(bts[i] >= 97 && bts[i] <= 122))
continue;
return false;
}
return true;
}
static string SqlStr = @"and|or|exec|execute|insert|select|delete|update|alter|create|drop|count|\*|chr|char|asc|mid|substring|master|truncate|declare|xp_cmdshell|restore|backup|net +user|net +localgroup +administrators";
///
/// Sql防注入
///
///
/// true 表示通过 fale 表示不通过
public static bool ProcessSqlStr(string inputString)
{
if (string.IsNullOrEmpty(inputString))
return true;
//string SqlStr =
try
{
if ((inputString != null) && (inputString != String.Empty))
{
string str_Regex = @"\b(" + SqlStr + @")\b";
Regex Regex = new Regex(str_Regex, RegexOptions.IgnoreCase);
//string s = Regex.Match(inputString).Value;
if (true == Regex.IsMatch(inputString))
return false;
}
}
catch
{
return false;
}
return true;
}
///
/// 字符串中是否包含SQL关键字
///
///
///
public static bool IsContainsSqlKey(string input)
{
if (string.IsNullOrEmpty(input))
{
return false;
}
if (input.IndexOf("'") >= 0) return false;
string str_Regex = @"\b(" + SqlStr + @")\b";
Regex Regex = new Regex(str_Regex, RegexOptions.IgnoreCase);
return Regex.IsMatch(input);
}
///
/// 检查字符串是否都是数字
///
///
///
public static bool CheckStringIsNum(string input)
{
if (string.IsNullOrEmpty(input))
return false;
int len = input.Length;
for (int i = 0; i < len; i++)
{
if (input[i] < 48 || input[i] > 57)
return false;
}
return true;
}
///
/// 检查字符串长度
///
/// 比较的字符串
/// 最小长度
/// 最大长度
/// true表示比较字符数量 false表示比较字节数量
/// 编码
/// 是否符合规则 true 表示符合
public static bool CheckStringLength(string input, int minLength, int maxLength, bool isCharLenth, Encoding encod)
{
if (input == null)
return false;
int len;
if (isCharLenth)
{
len = input.Length;
}
else
{
byte[] bts = encod.GetBytes(input);
len = bts.Length;
}
return len >= minLength && len <= maxLength;
}
///
/// 获取指定字节的字符串
///
/// 字符串
/// 保留的长度
/// 编码
///
public static string GetLengthString(string s, int count, Encoding encod)
{
if (string.IsNullOrEmpty(s))
return s;
char[] chars = s.ToCharArray();
if (chars.Length < count)
return s;
for (int i = count - 1; i >= 0; i--)
{
int strCount = encod.GetByteCount(s.ToCharArray(), 0, i + 1);//加上字符的长度
if (strCount <= count)
return s.Substring(0, i + 1);
}
return s;
}
///
/// 获取数组段组建的新数组
///
/// 数组段
/// 新数组
public static T[] GetNewArray(this ArraySegment ast)
{
T[] tarray = new T[ast.Count];
Array.Copy(ast.Array, ast.Offset, tarray, 0, ast.Count);
return tarray;
}
///
/// 获取数组的一段
///
///
///
///
///
public static T[] GetNewArray(this T[] array, int offset, int count)
{
T[] tarrray = new T[count];
Array.Copy(array, offset, tarrray, 0, count);
return tarrray;
}
///
/// 排序
///
///
///
///
///
public static void Sort(this int[] array, int left, int right, bool Asc = true)
{
for (int i = left; i < right; i++)
{
int index = i;
for (int j = i + 1; j <= right; j++)
{
if (Asc)
{
if (array[index] > array[j])
{
index = j;
}
}
else
{
if (array[index] < array[j])
{
index = j;
}
}
}
if (index != i)
{
int temp = array[i];
array[i] = array[index];
array[index] = temp;
}
}
}
///
/// 快速排序
///
/// 数组
///
///
///
///
public static void QuickSort(this int[] array, int left, int right, bool Asc = true)
{
if (left >= right)
return;
int index = left + 1; //下一个要替换的值
int i = index;
while (i <= right)
{
if (Asc)
{ //先把比第一个值大的放到第一个值前面
if (array[left] > array[i])
{
if (i != index)
{
int temp = array[index];
array[index] = array[i];
array[i] = temp;
}
index++;
}
}
else
{
if (array[left] < array[i])
{
if (i != index)
{
int temp = array[index];
array[index] = array[i];
array[i] = temp;
}
index++;
}
}
i++;
}
index--;
if (index != left)
{
int temp = array[index];
array[index] = array[left];
array[left] = temp;
}
array.QuickSort(left, index - 1, Asc);
array.QuickSort(index + 1, right, Asc);
}
///
/// 二分法查找
///
/// 数组
/// 值
///
///
/// 查找到返回其位置,没找到返回其应该在的位置的相反数-1
public static int FindIndex(this int[] array, int value, int left = 0, int right = -1)
{
if (array.Length == 0)
return -1;
if (right < 0)
right = array.Length - 1;
while (left <= right)
{
int midx = (left + right) / 2;
if (array[midx] > value)
right = midx - 1;
else if (array[midx] < value)
left = midx + 1;
else
return midx;
}
return -left - 1;
}
///
/// 根据概率获取是否成功
///
/// 概率 0 - 1
///
public static bool Probability(double value)
{
return random.NextDouble() < value;
}
///
/// 查找Jobject值
///
///
///
///
///
public static JToken Find(this JObject jobject, string key, bool isLow = true)
{
if (key == null)
return null;
if (!isLow)
return jobject[key];
var kl = key.ToLower();
foreach (var item in jobject.Properties())
{
if (item.Name.ToLower() == kl)
return item.Value;
}
return null;
}
///
/// 串连Jobject键值对
///
/// 要串联的jobject
/// 元素与元素之间的分隔符
/// 链接好的字符串
public static string Join(this JObject jobject, string separator = "&")
{
StringBuilder sb = new StringBuilder();
foreach (var item in jobject)
{
sb.Append(item.Key);
sb.Append("=");
sb.Append(item.Value);
sb.Append(separator);
}
if (sb.Length > 0)
sb.Remove(sb.Length - 1, 1);
return sb.ToString();
}
///
/// 串联IDictionary键值对
///
/// 要串联的键值对
/// 元素与元素之间的分割符
/// 链接好的字符串
public static string Join(this IDictionary dic, string separator = "&")
{
StringBuilder sb = new StringBuilder();
foreach (var item in dic.Keys)
{
sb.Append(item);
sb.Append("=");
sb.Append(dic[item]);
sb.Append(separator);
}
if (sb.Length > 0)
sb.Remove(sb.Length - 1, 1);
return sb.ToString();
}
///
/// 获取字符的长度 汉字按2字符算 英文字母1字符算
///
///
///
public static int GetSeatLength(string key)
{
return Encoding.GetEncoding("GB2312").GetByteCount(key);
}
///
/// 键值对对齐
///
/// key
/// value
/// 小于0 左对齐 大于0右对齐
///
public static string Align(string key, object value, int wight)
{
int bytelen = GetSeatLength(key);
int strlen = key.Length;
int count = bytelen - strlen;
if (count < Math.Abs(wight))
{
//左对齐
if (wight < 0)
{
wight += count;
}
else
{//右对齐
wight -= count;
}
}
string format = "{0," + wight + "}{1}";
return string.Format(format, key, value);
}
///
/// 发送post包裹
///
/// 请求地址
/// 数据
///
/// 结果
/// 超时时间 默认10秒钟
/// UA
///
public static HttpWebResponse HttpPost(string url, string body, ref string result, int sty = 1, int timeout = -1,string ua = null)
{
var tm0 = DateTime.Now;
if (timeout == -1)
timeout = 10000;
HttpWebResponse response = null;
Encoding encoding = Encoding.UTF8;
// url=HttpUtility.UrlEncode(url,encoding);
try
{
//ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.KeepAlive = false;
request.Accept = "text/html, application/xhtml+xml, */*";
if (!string.IsNullOrEmpty(ua))
{
request.UserAgent = ua;
}
switch (sty)
{
case 0:
request.ContentType = "application/x-www-form-urlencoded";
break;
case 1:
request.ContentType = "application/json";
break;
case 2:
request.ContentType = "application/octet-stream";
break;
}
request.Proxy = null;
request.CookieContainer = new CookieContainer();//接收cookies
if (timeout != -1)
request.Timeout = timeout;
request.ReadWriteTimeout = 15000;
byte[] buffer = encoding.GetBytes(body);
request.ContentLength = buffer.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(buffer, 0, buffer.Length);
}
response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
result = reader.ReadToEnd();
}
request.Abort();
}
catch (Exception e)
{
Console.WriteLine($"得到数据:{e.Message}");
return null;
}
return response;
}
///
/// httpGet请求
///
/// 地址
/// 结果
/// 超时时间
///
public static HttpWebResponse HttpGet(string url, ref string result, int timeout = -1)
{
if (timeout == -1)
timeout = 10000;
try
{
HttpWebResponse response = null;
Encoding encoding = Encoding.UTF8;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// request.KeepAlive=false;
request.Proxy = null;
request.Method = "GET";
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/json";
// switch(sty){
// case 0:request.ContentType = "application/x-www-form-urlencoded";break;
// case 1:request.ContentType = "application/json";break;
// case 2:request.ContentType = "application/octet-stream";break;
// }
// request.CookieContainer = new CookieContainer();//接收cookies
if (timeout > 0)
request.Timeout = timeout;
request.ReadWriteTimeout = 15000;
response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
result = reader.ReadToEnd();
}
request.Abort();
return response;
}
catch
{
return null;
}
}
catch (Exception)
{
return null;
}
}
///
/// 数据表是否为 null 或空表
///
/// 表
/// 返回结果
public static bool DataTableIsNullOrEmpty(DataTable table)
{
return table == null || table.Rows == null || table.Rows.Count == 0;
}
///
/// 判断数据集是否是 null 或者空数据集
///
///
///
public static bool DataSetIsNullOrEmpty(DataSet dataset)
{
return dataset == null || dataset.Tables == null || dataset.Tables.Count == 0;
}
///
/// 元素是否重复
///
///
///
public static bool ItemRepeat(IList list)
{
int len = list.Count;
IDictionary dic = new Dictionary();
for (int i = 0; i < len; i++)
{
if (dic.Contains(list[i]))
return true;
dic.Add(list[i], list[i]);
}
return false;
}
///
/// 元素是否重复
///
///
///
/// 比较器
///
public static bool ItemRepeat(IList list, Func thesame)
{
int len = list.Count;
for (int i = 0; i < len; i++)
{
for (int j = i + 1; j < len; j++)
{
if(thesame(list[i],list[j]))
return true;
}
}
return false;
}
}
}