private string Trim(string txt, Cut cut) { int s = cut.Start; int c = cut.Count; if (s > 0) { s -= 1; if (c == 0) { c = txt.Length; } else { c = s + c; } } else if (s < 0) { s += txt.Length + 1; if (c != 0) { c = s - c; } } if (s > c) { int t = s; s = c; c = t; } if (s >= txt.Length) { return txt; } if (c >= txt.Length) { return txt.Substring(0, s); } return txt.Substring(0, s) + txt.Substring(c); }
/// <summary> /// 字符裁剪 /// </summary> /// <param name="cmd"></param> /// <returns></returns> private bool DecodeCut(string cmd) { StringBuilder buffer = new StringBuilder(); if (!DecodeEnum(cmd, buffer, NUMBER)) { return false; } if (buffer.Length < 1) { Error = "无效的起始索引及长度数值!"; return false; } string[] arr = buffer.ToString().Split(NUMBER); Cut cut = new Cut(); string tmp; if (arr.Length > 0) { tmp = arr[0]; if (!CharUtil.IsValidateLong(tmp)) { Error = "无效的起始索引:" + tmp; return false; } cut.Start = int.Parse(tmp); if (cut.Start == 0) { Error = "起始索引不能为 0!"; return false; } } if (arr.Length > 1) { tmp = arr[1]; if (!string.IsNullOrWhiteSpace(tmp)) { if (!CharUtil.IsValidateLong(tmp)) { Error = "无效的长度数值:" + tmp; return false; } cut.Count = int.Parse(tmp); } } _CutList.Add(cut); return true; }