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.

173 lines
5.4 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Mesnac.Equips
{
/// <summary>
/// 设备工具类
/// </summary>
public class EquipTools
{
#region 根据设备组范围获取设备组名称数组
/// <summary>
/// 根据设备组范围获取设备组名称数组
/// </summary>
/// <param name="ruleStr">设备组范围值</param>
/// <returns>返回对应的设备组范围数组</returns>
public static string[] GetNameIndexFromRange(string ruleStr)
{
List<string> lstNameIndex = new List<string>();
if (CheckRuleColon(ruleStr))
{
string[] values = ruleStr.Split(new char[] { ':' });
int maxLen = GetMaxStrLen(values);
foreach (string value in values)
{
int intValue = 0;
int.TryParse(value, out intValue);
lstNameIndex.Add(FillZero(intValue.ToString(), maxLen));
}
}
else if (CheckRuleJoin(ruleStr))
{
string[] values = ruleStr.Split(new string[] { ".." }, StringSplitOptions.RemoveEmptyEntries); //去掉空值
int begin = 0;
int end = 0;
int.TryParse(values[0], out begin);
int.TryParse(values[1], out end);
int maxLen = end.ToString().Length;
for (int i = begin; i <= end; i++)
{
lstNameIndex.Add(FillZero(i.ToString(), maxLen));
}
}
return lstNameIndex.ToArray<string>();
}
#endregion
#region 设备组范围字符串格式验证方法
/// <summary>
/// 冒号分隔规则检测
/// </summary>
/// <param name="ruleStr">要检测的字符串</param>
/// <returns>符合规则返回true否则返回false</returns>
public static bool CheckRuleColon(string ruleStr)
{
bool result = true;
string[] values = ruleStr.Split(new char[] { ':' });
if (values.Length > 0)
{
foreach (string value in values)
{
if (!String.IsNullOrEmpty(value))
{
int intValue = 0;
if (!int.TryParse(value, out intValue))
{
result = false;
break;
}
}
}
}
else
{
result = false;
}
return result;
}
/// <summary>
/// 连接符分隔规则检测
/// </summary>
/// <param name="ruleStr">要检测的字符串</param>
/// <returns>符合规则返回true否则返回false</returns>
public static bool CheckRuleJoin(string ruleStr)
{
bool result = true;
string[] values = ruleStr.Split(new string[] { ".." }, StringSplitOptions.RemoveEmptyEntries); //去掉空值
if (values.Length == 2)
{
int intValue = 0;
if (int.TryParse(values[0], out intValue))
{
if (int.TryParse(values[1], out intValue))
{
result = true;
}
else
{
result = false;
}
}
else
{
result = false;
}
}
else
{
result = false;
}
return result;
}
#endregion
#region "按字符串位数补0"
/// <summary>
/// 按字符串位数补0
/// </summary>
/// <param name="CharTxt">字符串</param>
/// <param name="CharLen">字符长度</param>
/// <returns></returns>
public static string FillZero(string CharTxt, int CharLen)
{
if (CharTxt.Length < CharLen)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < CharLen - CharTxt.Length; i++)
{
sb.Append("0");
}
sb.Append(CharTxt);
return sb.ToString();
}
else
{
return CharTxt;
}
}
#endregion
#region 获取字符串数组中最长字符串的长度
/// <summary>
/// 获取字符串数组中最长字符串的长度
/// </summary>
/// <param name="strValues">字符串数组</param>
/// <returns>返回字符串数组中最长字符串的长度</returns>
public static int GetMaxStrLen(string[] strValues)
{
int maxLen = 0;
if (strValues != null && strValues.Length > 0)
{
foreach (string value in strValues)
{
if (value.Length > maxLen)
{
maxLen = value.Length;
}
}
}
return maxLen;
}
#endregion
}
}