中英文混合字符截取函数
/* C# 解决办法 */
/**
* get sub-string from a ANSI encoded string
* @param str ANSI encoded string
* @param offset the offset of string to be truncated
* @param len the length of sub-string
* @return sub-string truncated from @param str
*/
public static string GetSubString(string str, int offset, int len)
{
if (str == null || str == "") return str;
if (len <= 0) return "";
StringBuilder builder = new StringBuilder();
int j = 0, count = 0;
for (int i = 0; i < str.Length; i++)
{
if (j >= offset)
{
builder.Append(str[i]);
count++;
if ((((int)str[i]) & 0xff00) > 0) count++;
}
j++;
if ((((int)str[i]) & 0xff00) > 0) j++;
if (count >= len) break;
}
return builder.ToString();
}
/**
* get sub-string from a ANSI encoded string
* @param str ANSI encoded string
* @param offset the offset of string to be truncated
* @param len the length of sub-string
* @return sub-string truncated from @param str
*/
public static string GetSubString(string str, int offset, int len)
{
if (str == null || str == "") return str;
if (len <= 0) return "";
StringBuilder builder = new StringBuilder();
int j = 0, count = 0;
for (int i = 0; i < str.Length; i++)
{
if (j >= offset)
{
builder.Append(str[i]);
count++;
if ((((int)str[i]) & 0xff00) > 0) count++;
}
j++;
if ((((int)str[i]) & 0xff00) > 0) j++;
if (count >= len) break;
}
return builder.ToString();
}
'ASP解决方法
Function msglength(msg,length)
if msg="" or isnull(msg) or IsNumeric(length)=False then
msglength=""
Exit Function
else
length=int(length)
j=0
for ii=1 to len(msg)
if asc(mid(msg,ii,1))>0 then j=j+1 else j=j+2
if j>length*2 then exit for
next
if j<=length*2 then msglength=msg else msglength=left(msg,ii-1)&""
end if
End function
Function msglength(msg,length)
if msg="" or isnull(msg) or IsNumeric(length)=False then
msglength=""
Exit Function
else
length=int(length)
j=0
for ii=1 to len(msg)
if asc(mid(msg,ii,1))>0 then j=j+1 else j=j+2
if j>length*2 then exit for
next
if j<=length*2 then msglength=msg else msglength=left(msg,ii-1)&""
end if
End function