[\u0391-\uFFE5]匹配双字节字符(汉字+符号)
[\u4e00-\u9fa5]注意只匹配汉字,不匹配双字节字符
代码
/// <summary>
/// 截取指定长度的字符串(Unicode占两位)
/// </summary>
/// <param name="source"> 指定字符串 </param>
/// <param name="length"> 指定长度 </param>
/// <returns> 返回指定字符串长度的 </returns>
public static string Sub( string source, int length)
{
string temp = source;
if (Regex.Replace(temp, " [\u0391-\uffe5] " , " " , RegexOptions.IgnoreCase).Length <= length)
{
return temp;
}
for ( int i = temp.Length; i >= 0 ; i -- )
{
temp = temp.Substring( 0 , i);
if (Regex.Replace(temp, " [\u0391-\uffe5] " , " " , RegexOptions.IgnoreCase).Length <= length)
{
return temp + "" ;
}
}
return "" ;
}
/// <summary>
/// 截取指定长度的字符串(Unicode占两位)
/// </summary>
/// <param name="source"> 指定字符串 </param>
/// <param name="length"> 指定长度 </param>
/// <returns> 返回指定字符串长度的 </returns>
public static string Sub( string source, int length)
{
string temp = source;
if (Regex.Replace(temp, " [\u0391-\uffe5] " , " " , RegexOptions.IgnoreCase).Length <= length)
{
return temp;
}
for ( int i = temp.Length; i >= 0 ; i -- )
{
temp = temp.Substring( 0 , i);
if (Regex.Replace(temp, " [\u0391-\uffe5] " , " " , RegexOptions.IgnoreCase).Length <= length)
{
return temp + "" ;
}
}
return "" ;
}
上面这种执行效率比下面这种方法要高,耗时要短:
代码
///
<summary>
/// 截取指定长度的字符串(Unicode占两位)
/// </summary>
/// <param name="source"> 指定字符串 </param>
/// <param name="length"> 指定长度 </param>
/// <returns> 返回指定字符串长度的 </returns>
public static string SLeft( string source, int length)
{
Regex regex = new Regex( " [\u0391-\uffe5]+ " , RegexOptions.Compiled);
char [] stringChar = source.ToCharArray();
string sb = "" ;
int nLength = 0 ;
for ( int i = 0 ; i < stringChar.Length; i ++ )
{
if (nLength >= length)
{
break ;
}
if (regex.IsMatch((stringChar[i]).ToString()))
{
sb += stringChar[i];
nLength += 2 ;
}
else
{
sb += stringChar[i];
nLength = nLength + 1 ;
}
}
return sb.ToString();
}
/// 截取指定长度的字符串(Unicode占两位)
/// </summary>
/// <param name="source"> 指定字符串 </param>
/// <param name="length"> 指定长度 </param>
/// <returns> 返回指定字符串长度的 </returns>
public static string SLeft( string source, int length)
{
Regex regex = new Regex( " [\u0391-\uffe5]+ " , RegexOptions.Compiled);
char [] stringChar = source.ToCharArray();
string sb = "" ;
int nLength = 0 ;
for ( int i = 0 ; i < stringChar.Length; i ++ )
{
if (nLength >= length)
{
break ;
}
if (regex.IsMatch((stringChar[i]).ToString()))
{
sb += stringChar[i];
nLength += 2 ;
}
else
{
sb += stringChar[i];
nLength = nLength + 1 ;
}
}
return sb.ToString();
}
下面这种方法耗时最短,效率最高:
代码
public
static
string
GetStr(
string
source,
int
length)
{
int count = 0 ;
for ( int i = 0 ; i < source.Length; i ++ )
{
if ((( int )source[i]).ToString( " X2 " ).Length == 4 )
count += 2 ;
else
count += 1 ;
}
if (count > length)
{
StringBuilder sb = new StringBuilder();
count = 0 ;
for ( int i = 0 ; i < source.Length; i ++ )
{
char temp = source[i];
if ((( int )temp).ToString( " X2 " ).Length == 4 )
{
count += 2 ;
}
else
{
count += 1 ;
}
if (count < length)
{
sb.Append(temp);
}
else if (count == length)
{
sb.Append(temp);
break ;
}
else if (count > length)
{
sb.Append( "" );
break ;
}
}
return sb.ToString();
}
else
{
return source;
}
}
{
int count = 0 ;
for ( int i = 0 ; i < source.Length; i ++ )
{
if ((( int )source[i]).ToString( " X2 " ).Length == 4 )
count += 2 ;
else
count += 1 ;
}
if (count > length)
{
StringBuilder sb = new StringBuilder();
count = 0 ;
for ( int i = 0 ; i < source.Length; i ++ )
{
char temp = source[i];
if ((( int )temp).ToString( " X2 " ).Length == 4 )
{
count += 2 ;
}
else
{
count += 1 ;
}
if (count < length)
{
sb.Append(temp);
}
else if (count == length)
{
sb.Append(temp);
break ;
}
else if (count > length)
{
sb.Append( "" );
break ;
}
}
return sb.ToString();
}
else
{
return source;
}
}
测试代码如下:
代码
string
source
=
"
《中国》按时打发按时打发撒地方啊飒飒asdfasdfasd的法师打发似的<发(生的发生)地方按时打发是asdfasdfas
"
;
int length = 10 ;
DateTime dt = DateTime.Now;
for ( int i = 0 ; i < 1000 ; i ++ )
{
string temp = StrFun.SLeft(source, length);
}
TimeSpan ts = DateTime.Now - dt;
Console.WriteLine(ts.TotalMilliseconds);
dt = DateTime.Now;
for ( int i = 0 ; i < 1000 ; i ++ )
{
string temp = StrFun.Sub(source, length);
}
ts = DateTime.Now - dt;
Console.WriteLine(ts.TotalMilliseconds);
dt = DateTime.Now;
for ( int i = 0 ; i < 1000 ; i ++ )
{
string temp = StrFun.GetStr(source, length);
}
ts = DateTime.Now - dt;
Console.WriteLine(ts.TotalMilliseconds);
Console.Read();
int length = 10 ;
DateTime dt = DateTime.Now;
for ( int i = 0 ; i < 1000 ; i ++ )
{
string temp = StrFun.SLeft(source, length);
}
TimeSpan ts = DateTime.Now - dt;
Console.WriteLine(ts.TotalMilliseconds);
dt = DateTime.Now;
for ( int i = 0 ; i < 1000 ; i ++ )
{
string temp = StrFun.Sub(source, length);
}
ts = DateTime.Now - dt;
Console.WriteLine(ts.TotalMilliseconds);
dt = DateTime.Now;
for ( int i = 0 ; i < 1000 ; i ++ )
{
string temp = StrFun.GetStr(source, length);
}
ts = DateTime.Now - dt;
Console.WriteLine(ts.TotalMilliseconds);
Console.Read();
结果为:
2328.125
1515.625
15.625