![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**////str_value 字符
///str_len 要截取的字符长度
public string leftx(string str_value,int str_len)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int p_num = 0;
int i;
string New_Str_value = "";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (str_value=="")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
New_Str_value = "";
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int Len_Num = str_value.Length;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//if (Len_Num < str_len)
//{
// str_len = Len_Num;
//}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (i = 0;i<=Len_Num - 1; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//str_value.Substring(i,1);
if (i >Len_Num) break;
char c = Convert.ToChar(str_value.Substring(i,1));
if (((int)c > 255) || ((int)c<0))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
p_num = p_num + 2;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
p_num = p_num + 1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (p_num >= str_len)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
New_Str_value = str_value.Substring(0,i+1);
break;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
New_Str_value = str_value;
}
}
}
return New_Str_value;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
C#截取中英文混合字符串
注意:需要引用正则名字空间,using System.Text.RegularExpressions;
调用方法:
CnCutString(ArtTitle, 24, "..");
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 截取中英文混合字符串
/// </summary>
/// <param name="original">原始字符串</param>
/// <param name="length">截取长度</param>
/// <param name="fill">截取串小于原始串时,尾部附加字符串</param>
/// <returns></returns>
public static String CnCutString(String original, Int32 length, String fill)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Regex CnRegex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
Char[] CharArray = original.ToCharArray();
Int32 tmplength = 0;
for (Int32 i = 0; i < CharArray.Length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tmplength += CnRegex.IsMatch(CharArray[i].ToString()) ? 2 : 1;
if (tmplength > length) return original.Substring(0, i - fill.Length) + fill;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return original;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)