You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

311 lines
9.1 KiB
C#

9 months ago
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
namespace POSV.Utils
{
public class StringUtils
{
//判断字符串是否为纯数字
public static bool IsNumber(string str)
{
if (str == null || str.Length == 0)
return false;
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] bytestr = ascii.GetBytes(str);
foreach (byte c in bytestr)
{
if (c < 48 || c > 57)
{
return false;
}
}
return true;
}
public static bool isNumberic(string message, out int result)
{
System.Text.RegularExpressions.Regex rex =
new System.Text.RegularExpressions.Regex(@"^\d+$");
result = -1;
if (rex.IsMatch(message))
{
result = int.Parse(message);
return true;
}
else
{
return false;
}
}
/// <summary>
/// 清除字典中值为空的项。
/// </summary>
/// <param name="dict">待清除的字典</param>
/// <returns>清除后的字典</returns>
public static IDictionary<string, T> CleanupDictionary<T>(IDictionary<string, T> dict)
{
IDictionary<string, T> newDict = new Dictionary<string, T>(dict.Count);
IEnumerator<KeyValuePair<string, T>> dem = dict.GetEnumerator();
while (dem.MoveNext())
{
string name = dem.Current.Key;
T value = dem.Current.Value;
if (value != null)
{
newDict.Add(name, value);
}
}
return newDict;
}
/// <summary>
/// 获取文件的真实后缀名。目前只支持JPG, GIF, PNG, BMP四种图片文件。
/// </summary>
/// <param name="fileData">文件字节流</param>
/// <returns>JPG, GIF, PNG or null</returns>
public static string GetFileSuffix(byte[] fileData)
{
if (fileData == null || fileData.Length < 10)
{
return null;
}
if (fileData[0] == 'G' && fileData[1] == 'I' && fileData[2] == 'F')
{
return "GIF";
}
else if (fileData[1] == 'P' && fileData[2] == 'N' && fileData[3] == 'G')
{
return "PNG";
}
else if (fileData[6] == 'J' && fileData[7] == 'F' && fileData[8] == 'I' && fileData[9] == 'F')
{
return "JPG";
}
else if (fileData[0] == 'B' && fileData[1] == 'M')
{
return "BMP";
}
else
{
return null;
}
}
/// <summary>
/// 获取文件的真实媒体类型。目前只支持JPG, GIF, PNG, BMP四种图片文件。
/// </summary>
/// <param name="fileData">文件字节流</param>
/// <returns>媒体类型</returns>
public static string GetMimeType(byte[] fileData)
{
string suffix = GetFileSuffix(fileData);
string mimeType;
switch (suffix)
{
case "JPG": mimeType = "image/jpeg"; break;
case "GIF": mimeType = "image/gif"; break;
case "PNG": mimeType = "image/png"; break;
case "BMP": mimeType = "image/bmp"; break;
default: mimeType = "application/octet-stream"; break;
}
return mimeType;
}
/// <summary>
/// 根据文件后缀名获取文件的媒体类型。
/// </summary>
/// <param name="fileName">带后缀的文件名或文件全名</param>
/// <returns>媒体类型</returns>
public static string GetMimeType(string fileName)
{
string mimeType;
fileName = fileName.ToLower();
if (fileName.EndsWith(".bmp", StringComparison.CurrentCulture))
{
mimeType = "image/bmp";
}
else if (fileName.EndsWith(".gif", StringComparison.CurrentCulture))
{
mimeType = "image/gif";
}
else if (fileName.EndsWith(".jpg", StringComparison.CurrentCulture) || fileName.EndsWith(".jpeg", StringComparison.CurrentCulture))
{
mimeType = "image/jpeg";
}
else if (fileName.EndsWith(".png", StringComparison.CurrentCulture))
{
mimeType = "image/png";
}
else
{
mimeType = "application/octet-stream";
}
return mimeType;
}
public static string GetString(object obj)
{
return GetString(obj, string.Empty);
}
public static string GetString(object obj, string defaultString)
{
return obj != null ? obj.ToString() : defaultString;
}
public static int GetInt(object obj)
{
return GetInt(obj, 0);
}
public static int GetInt(object obj, int defaulVale)
{
int _value = defaulVale;
int.TryParse((obj != null ? obj.ToString() : defaulVale.ToString()), out _value);
return _value;
}
public static double GetDouble(object obj)
{
return GetDouble(obj, 0);
}
public static double GetDouble(object obj, double defaulVale)
{
double _value = defaulVale;
double.TryParse((obj != null ? obj.ToString() : defaulVale.ToString()), out _value);
return _value;
}
public static decimal GetDecimal(object obj)
{
return GetDecimal(obj, 0);
}
public static decimal GetDecimal(object obj, decimal defaulVale)
{
decimal _value = defaulVale;
decimal.TryParse((obj != null ? obj.ToString() : defaulVale.ToString()), out _value);
return _value;
}
static Regex unicode = new Regex(@"\\u([0-9a-fA-F]{4})", RegexOptions.Compiled);
public static string DecodeUnicode(string s)
{
return unicode.Replace(s, m =>
{
short c;
if (short.TryParse(m.Groups[1].Value, System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture, out c))
{
return "" + (char)c;
}
return m.Value;
});
}
/// <summary>
/// 格式化显示数字
/// </summary>
/// <param name="num">数字</param>
/// <param name="digit">保留小数位数</param>
/// <returns></returns>
public static string FormatDataSwitch(decimal num, int digit)
{
string result = string.Empty;
switch (digit)
{
case 0:
result = FormatDataNoDigit(num);
break;
case 1:
result = FormatDataOneDigit(num);
break;
case 2:
default:
result = FormatDataTwoDigit(num);
break;
}
return result;
}
public static string FormatDataNoDigit(decimal num)
{
return FormatData(num, "0.##");
}
public static string FormatDataOneDigit(decimal num)
{
return FormatData(num, "0.0#");
}
public static string FormatDataTwoDigit(decimal num)
{
return FormatData(num, "f2");
}
/// <summary>
/// 对象格式化两位小数
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static decimal FormatDataTwoDigitObject(object num)
{
try
{
return GetDecimal(FormatDataTwoDigit(GetDecimal(num)));
}
catch (Exception e)
{
return 0.00M;
}
}
public static string FormatData(decimal num, string format)
{
return num.ToString(format);
}
/// <summary>
/// 判断是否是手机号码
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsMobilePhone(string input)
{
Regex regex = new Regex("^1[3456789]\\d{9}$");
return regex.IsMatch(input);
}
/// <summary>
/// 判断整数int)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsInteger(String str)
{
if (string.IsNullOrEmpty(str))
{
return false;
}
Regex regex = new Regex("^[-\\+]?[\\d]*$");
return regex.IsMatch(str);
}
}
}