1 /// <summary> 2 /// 截取传入的字符串中左侧第 num 个返回. 3 /// </summary> 4 /// <param name="s">要截取的字符串。</param> 5 /// <param name="num">从左侧数起,截取的字符个数。</param> 6 /// <param name="code">当传入的字符数长于截取字符数是,在已截取字符串加上此字符(如“...”)</param> 7 /// <returns></returns> 8 public static string getvalue(string s,int num) 9 { 10 char[] arr = s.ToCharArray(); 11 if (s.Length > num) 12 { 13 if (arr[num - 1] == ' ') 14 { 15 return s.Substring(0,num-1); 16 } 17 else if (arr[num] == ' ') 18 { 19 20 return s.Substring(0,num); 21 } 22 else 23 { 24 int index = s.Substring(0, num).LastIndexOf(" "); 25 string str = s.Substring(0, num); 26 return str.Substring(0, str.LastIndexOf(" ")); 27 } 28 } 29 else 30 { 31 return s; 32 } 33 } 34 public static string getvalue(string s, int num,string code) 35 { 36 char[] arr = s.ToCharArray(); 37 if (s.Length > num) 38 { 39 if (arr[num - 1] == ' ') 40 { 41 return s.Substring(0, num - 1)+code; 42 } 43 else if (arr[num] == ' ') 44 { 45 return s.Substring(0, num)+code; 46 } 47 else 48 { 49 int index = s.Substring(0, num).LastIndexOf(" "); 50 string str = s.Substring(0, num); 51 return str.Substring(0, str.LastIndexOf(" "))+code; 52 } 53 } 54 else 55 { 56 return s; 57 } 58 }