public class StringUtility {
public static string BreakLongString(string SubjectString, int lineLength)
{
StringBuilder sb = new StringBuilder(SubjectString);
int offset = 0;
ArrayList indexList = buildInsertIndexList(SubjectString, lineLength);
for(int i=0;i<indexList.Count;i++){
sb.Insert((int)indexList[i]+offset,'\n');
offset++;
}
return sb.ToString();
}
public static bool IsChinese(char c){
return (int)c>=0x4E00 && (int)c<=0x9FA5;
}
private static ArrayList buildInsertIndexList(string str, int maxLen){
int nowLen = 0;
ArrayList list = new ArrayList();
for(int i=1;i<str.Length;i++){
if(IsChinese(str[i])){
nowLen += 2;
}else{
nowLen ++;
}
if(nowLen > maxLen){
nowLen = 0;
list.Add(i);
}
}
return list;
}
}
C# 在指定字符串中间插入换行符
最新推荐文章于 2024-10-19 22:20:03 发布