將內(nèi)容過程中比較常用的內(nèi)容備份一下,下面的內(nèi)容是關(guān)于C#區(qū)分中英文按照指定長度截取字符串的內(nèi)容,應(yīng)該能對(duì)小伙伴有所好處。
? ? ? ? public static string GetSubString(string str, int length)
? ? ? ? {
? ? ? ? ? ? string temp = str;
? ? ? ? ? ? int j = 0;
? ? ? ? ? ? int k = 0;
? ? ? ? ? ? for (int i = 0; i < temp.Length; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (Regex.IsMatch(temp.Substring(i, 1), @"[u4e00-u9fa5]+"))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? j += 2;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? j += 1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (j <= length)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? k += 1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (j > length)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? return temp.Substring(0, k) + "..";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return temp;
? ? ? ? }