using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ropin.Inspection.Common.Helper
{
public class StringHelper
{
///
/// 根据分隔符返回前n条数据
///
/// 数据内容
/// 分隔符
/// 前n条
/// 是否倒序(默认false)
///
public static List GetTopDataBySeparator(string content, string separator, int top, bool isDesc = false)
{
if (string.IsNullOrEmpty(content))
{
return new List() { };
}
if (string.IsNullOrEmpty(separator))
{
throw new ArgumentException("message", nameof(separator));
}
var dataArray = content.Split(separator).Where(d => !string.IsNullOrEmpty(d)).ToArray();
if (isDesc)
{
Array.Reverse(dataArray);
}
if (top > 0)
{
dataArray = dataArray.Take(top).ToArray();
}
return dataArray.ToList();
}
///
/// 根据字段拼接get参数
///
///
///
public static string GetPars(Dictionary dic)
{
StringBuilder sb = new StringBuilder();
string urlPars = null;
bool isEnter = false;
foreach (var item in dic)
{
sb.Append($"{(isEnter ? "&" : "")}{item.Key}={item.Value}");
isEnter = true;
}
urlPars = sb.ToString();
return urlPars;
}
///
/// 根据字段拼接get参数
///
///
///
public static string GetPars(Dictionary dic)
{
StringBuilder sb = new StringBuilder();
string urlPars = null;
bool isEnter = false;
foreach (var item in dic)
{
sb.Append($"{(isEnter ? "&" : "")}{item.Key}={item.Value}");
isEnter = true;
}
urlPars = sb.ToString();
return urlPars;
}
///
/// 获取一个GUID
///
/// 格式-默认为N
///
public static string GetGUID(string format = "N")
{
return Guid.NewGuid().ToString(format);
}
///
/// 根据GUID获取19位的唯一数字序列
///
///
public static long GetGuidToLongID()
{
byte[] buffer = Guid.NewGuid().ToByteArray();
return BitConverter.ToInt64(buffer, 0);
}
///
/// 根据GUID获取16位的唯一字符串
///
///
///
public static string GuidTo16String(Guid guid)
{
long i = 1;
foreach (byte b in guid.ToByteArray())
i *= ((int)b + 1);
return string.Format("{0:x}", i - DateTime.Now.Ticks);
}
///
/// 根据GUID获取19位的唯一数字序列
///
///
public static long GuidToLongID(Guid guid)
{
byte[] buffer = guid.ToByteArray();
return BitConverter.ToInt64(buffer, 0);
}
///
/// 获取字符串最后X行
///
///
///
///
public static string GetCusLine(string resourceStr, int length)
{
string[] arrStr = resourceStr.Split("\r\n");
return string.Join("", (from q in arrStr select q).Skip(arrStr.Length - length + 1).Take(length).ToArray());
}
#region 删除最后结尾的一个逗号
///
/// 删除最后结尾的一个逗号
///
public static string DelLastComma(string str)
{
if (str.Length < 1)
{
return "";
}
return str.Substring(0, str.LastIndexOf(","));
}
#endregion 删除最后结尾的一个逗号
#region 删除最后结尾的指定字符后的字符
///
/// 删除最后结尾的指定字符后的字符
///
public static string DelLastChar(string str, string strchar)
{
if (string.IsNullOrEmpty(str))
return "";
if (str.LastIndexOf(strchar) >= 0 && str.LastIndexOf(strchar) == str.Length - 1)
{
return str.Substring(0, str.LastIndexOf(strchar));
}
return str;
}
#endregion 删除最后结尾的指定字符后的字符
#region 删除最后结尾的一个逗号及字符
///
/// 删除最后结尾的一个逗号
///
public static string DelLastCommaStr(string str)
{
if (str.Length < 1)
{
return "";
}
return str.Substring(0, str.LastIndexOf(","));
}
#endregion 删除最后结尾的一个逗号
}
}