1 #region 从字符串的指定位置截取指定长度的子字符串 2 /// <summary> 3 /// 从字符串的指定位置截取指定长度的子字符串 4 /// </summary> 5 /// <param name="str">原字符串</param> 6 /// <param name="startIndex">子字符串的起始位置</param> 7 /// <param name="length">子字符串的长度</param> 8 /// <returns>子字符串</returns> 9 private static string CutString(string str, int startIndex, int length) 10 { 11 if (startIndex >= 0) 12 { 13 if (length < 0) 14 { 15 length = length * -1; 16 if (startIndex - length < 0) 17 { 18 length = startIndex; 19 startIndex = 0; 20 } 21 else 22 { 23 startIndex = startIndex - length; 24 } 25 } 26 27 if (startIndex > str.Length) 28 { 29 return ""; 30 } 31 } 32 else 33 { 34 if (length < 0) 35 { 36 return ""; 37 } 38 else 39 { 40 if (length + startIndex > 0) 41 { 42 length = length + startIndex; 43 startIndex = 0; 44 } 45 else 46 { 47 return ""; 48 } 49 } 50 } 51 52 if (str.Length - startIndex < length) 53 { 54 length = str.Length - startIndex; 55 } 56 57 return str.Substring(startIndex, length); 58 } 59 60 /// <summary> 61 /// 从字符串的指定位置开始截取到字符串结尾的了符串 62 /// </summary> 63 /// <param name="str">原字符串</param> 64 /// <param name="startIndex">子字符串的起始位置</param> 65 /// <returns>子字符串</returns> 66 private static string CutString(string str, int startIndex) 67 { 68 return CutString(str, startIndex, str.Length); 69 } 70 #endregion