C截取定长字符串函数
C#截取定长字符串函数
/// summary
/// °×¤¨¤×
/// /summary
/// param name=“str"-×/param
/// param name="length"¤/param
/// returnsstring×/returns
public static string CutStr(string str, int length)
{
int i = 0, j = 0;
foreach (char chr in str)
{
if ((int)chr 127)
{
i += 2;
}
else
{
i++;
}
if (i length)
{
str = str.Substring(0, j) + "...";
break;
}
j++;
}
return str;
}
//¨
public string CutStr(string sInString,int iCutLength)
{
if(sInString==null || sInString.Length==0 || iCutLength=0) {
return "";
}
int
iCount=System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(sInString);
if(iCountiCutLength)
{
int iLength=0;
C#截取定长字符串函数
for(int i=0;isInString.Length;i++)
{
int
iCharLength=System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(new char[]{sInString[i]});
iLength += iCharLength;
if(iLength==iCutLength)
{
sInString=sInString.Substring(0,i+1);
break;
}
else if(iLengthiCutLength)
{
sInString=sInString.Substring(0,i);
break;
}
}
}
return sInString;
}
//¨ò±í××
public static string CutStr(string stringToSub, int length) {
Regex regex = new Regex("[\u4e00-\u9fa5]+", piled);
char[] stringChar = stringToSub.ToCharArray();
StringBuilder sb = new StringBuilder();
int nLength = 0;c++求字符串长度
for (int i = 0; i stringChar.Length; i++)
{
if (regex.IsMatch((stringChar[i]).ToString())) {
nLength += 2;
}
else
{
nLength = nLength + 1;
}
if (nLength = length)
{
sb.Append(stringChar[i]);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论