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

260 lines
7.0 KiB

using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace EC.Utils
{
/// <summary>
/// 字符串操作 - 工具方法
/// </summary>
public sealed partial class Str
{
#region Empty(空字符串)
/// <summary>
/// 空字符串
/// </summary>
public static string Empty
{
get { return string.Empty; }
}
#endregion Empty(空字符串)
#region PinYin(获取汉字的拼音简码)
/// <summary>
/// 使用字符编码方式获取拼音简码
/// </summary>
private static string ResolvePinYinByCode(ushort unicode)
{
if (unicode >= '\uB0A1' && unicode <= '\uB0C4')
return "A";
if (unicode >= '\uB0C5' && unicode <= '\uB2C0' && unicode != 45464)
return "B";
if (unicode >= '\uB2C1' && unicode <= '\uB4ED')
return "C";
if (unicode >= '\uB4EE' && unicode <= '\uB6E9')
return "D";
if (unicode >= '\uB6EA' && unicode <= '\uB7A1')
return "E";
if (unicode >= '\uB7A2' && unicode <= '\uB8C0')
return "F";
if (unicode >= '\uB8C1' && unicode <= '\uB9FD')
return "G";
if (unicode >= '\uB9FE' && unicode <= '\uBBF6')
return "H";
if (unicode >= '\uBBF7' && unicode <= '\uBFA5')
return "J";
if (unicode >= '\uBFA6' && unicode <= '\uC0AB')
return "K";
if (unicode >= '\uC0AC' && unicode <= '\uC2E7')
return "L";
if (unicode >= '\uC2E8' && unicode <= '\uC4C2')
return "M";
if (unicode >= '\uC4C3' && unicode <= '\uC5B5')
return "N";
if (unicode >= '\uC5B6' && unicode <= '\uC5BD')
return "O";
if (unicode >= '\uC5BE' && unicode <= '\uC6D9')
return "P";
if (unicode >= '\uC6DA' && unicode <= '\uC8BA')
return "Q";
if (unicode >= '\uC8BB' && unicode <= '\uC8F5')
return "R";
if (unicode >= '\uC8F6' && unicode <= '\uCBF9')
return "S";
if (unicode >= '\uCBFA' && unicode <= '\uCDD9')
return "T";
if (unicode >= '\uCDDA' && unicode <= '\uCEF3')
return "W";
if (unicode >= '\uCEF4' && unicode <= '\uD188')
return "X";
if (unicode >= '\uD1B9' && unicode <= '\uD4D0')
return "Y";
if (unicode >= '\uD4D1' && unicode <= '\uD7F9')
return "Z";
return string.Empty;
}
#endregion PinYin(获取汉字的拼音简码)
#region Splice(拼接集合元素)
/// <summary>
/// 拼接集合元素
/// </summary>
/// <typeparam name="T">集合元素类型</typeparam>
/// <param name="list">集合</param>
/// <param name="quotes">引号,默认不带引号,范例:单引号 "'"</param>
/// <param name="separator">分隔符,默认使用逗号分隔</param>
public static string Splice<T>(IEnumerable<T> list, string quotes = "", string separator = ",")
{
if (list == null)
return string.Empty;
var result = new StringBuilder();
foreach (var each in list)
result.AppendFormat("{0}{1}{0}{2}", quotes, each, separator);
return result.ToString().TrimEnd(separator.ToCharArray());
}
#endregion Splice(拼接集合元素)
#region FirstUpper(将值的首字母大写)
/// <summary>
/// 将值的首字母大写
/// </summary>
/// <param name="value">值</param>
public static string FirstUpper(string value)
{
string firstChar = value.Substring(0, 1).ToUpper();
return firstChar + value.Substring(1, value.Length - 1);
}
#endregion FirstUpper(将值的首字母大写)
#region ToCamel(将字符串转成驼峰形式)
/// <summary>
/// 将字符串转成驼峰形式
/// </summary>
/// <param name="value">原始字符串</param>
public static string ToCamel(string value)
{
return FirstUpper(value.ToLower());
}
#endregion ToCamel(将字符串转成驼峰形式)
#region ContainsChinese(是否包含中文)
/// <summary>
/// 是否包含中文
/// </summary>
/// <param name="text">文本</param>
public static bool ContainsChinese(string text)
{
const string pattern = "[\u4e00-\u9fa5]+";
return Regex.IsMatch(text, pattern);
}
#endregion ContainsChinese(是否包含中文)
#region ContainsNumber(是否包含数字)
/// <summary>
/// 是否包含数字
/// </summary>
/// <param name="text">文本</param>
public static bool ContainsNumber(string text)
{
const string pattern = "[0-9]+";
return Regex.IsMatch(text, pattern);
}
#endregion ContainsNumber(是否包含数字)
#region Distinct(去除重复)
/// <summary>
/// 去除重复
/// </summary>
/// <param name="value">值,范例1:"5555",返回"5",范例2:"4545",返回"45"</param>
public static string Distinct(string value)
{
var array = value.ToCharArray();
return new string(array.Distinct().ToArray());
}
#endregion Distinct(去除重复)
#region Truncate(截断字符串)
/// <summary>
/// 截断字符串
/// </summary>
/// <param name="text">文本</param>
/// <param name="length">返回长度</param>
/// <param name="endCharCount">添加结束符号的个数,默认0,不添加</param>
/// <param name="endChar">结束符号,默认为省略号</param>
public static string Truncate(string text, int length, int endCharCount = 0, string endChar = ".")
{
if (string.IsNullOrWhiteSpace(text))
return string.Empty;
if (text.Length < length)
return text;
return text.Substring(0, length) + GetEndString(endCharCount, endChar);
}
/// <summary>
/// 获取结束字符串
/// </summary>
private static string GetEndString(int endCharCount, string endChar)
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < endCharCount; i++)
result.Append(endChar);
return result.ToString();
}
#endregion Truncate(截断字符串)
#region ToSimplifiedChinese(转换为简体中文)
/// <summary>
/// 转换为简体中文
/// </summary>
/// <param name="text">繁体中文</param>
public static string ToSimplifiedChinese(string text)
{
return Strings.StrConv(text, VbStrConv.SimplifiedChinese);
}
#endregion ToSimplifiedChinese(转换为简体中文)
#region ToSimplifiedChinese(转换为繁体中文)
/// <summary>
/// 转换为繁体中文
/// </summary>
/// <param name="text">简体中文</param>
public static string ToTraditionalChinese(string text)
{
return Strings.StrConv(text, VbStrConv.TraditionalChinese);
}
#endregion ToSimplifiedChinese(转换为繁体中文)
#region Unique(获取全局唯一值)
/// <summary>
/// 获取全局唯一值
/// </summary>
public static string Unique()
{
return Guid.NewGuid().ToString().Replace("-", "");
}
#endregion Unique(获取全局唯一值)
#region GetLastProperty(获取最后一个属性)
/// <summary>
/// 获取最后一个属性
/// </summary>
/// <param name="propertyName">属性名,范例,A.B.C,返回"C"</param>
public static string GetLastProperty(string propertyName)
{
if (string.IsNullOrEmpty(propertyName))
return string.Empty;
var lastIndex = propertyName.LastIndexOf(".", StringComparison.Ordinal) + 1;
return propertyName.Substring(lastIndex);
}
#endregion GetLastProperty(获取最后一个属性)
}
}