#region 不拆散双字节的字符,按间隔要求插入字符串
/// <summary>
/// 不拆散双字节的字符,按间隔要求插入字符串
/// </summary>
/// <param name="txt">原始字符串</param>
/// <param name="space">要插入字符串的间隔数,是字符数</param>
/// <param name="spaceMark">要插入的字符串</param>
/// <returns></returns>
private string InsertMark(string txt, int space, string spaceMark)
{
int maxLen = System.Text.Encoding.Default.GetByteCount(txt);
int markLen = spaceMark.Length;
int beginIndex = 0;
for (int nowIndex = space; nowIndex < maxLen; nowIndex = beginIndex + space)
{
int nowLen = txt.Length; //取当前的字符串长度
if (nowIndex > nowLen) nowIndex = nowLen; //修正现在要截取的字符串的最后索引位,剩下的字符串不足space个字符时,取到字符串最后
string tempTxt = txt.Substring(beginIndex, nowIndex - beginIndex); //取临时字符串,用于在下面循环判断是否符合字符数要求
if (nowLen == beginIndex && nowLen == nowIndex) break; //无字符串可截取了,退出循环
//如果截取的临时字符串的字符数大于间隔字符数,往前取一个字符,直到符合间隔字符数的要求
while (System.Text.Encoding.Default.GetByteCount(tempTxt) > space)
{
nowIndex--;
tempTxt = txt.Substring(beginIndex, nowIndex - beginIndex);
}
txt = txt.Insert(nowIndex, spaceMark); //插入间隔字符
beginIndex = nowIndex + markLen; //后移下次循环时临时字符串的起始位置
maxLen += markLen; //修正当前字符串的总长度,保证继续循环
}
return txt;
}
#endregion