C截取定长字符串函数

更新时间:2023-07-28 02:57:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

C#截取定长字符串函数

/// <summary>

/// °´×Ö·û´®Êµ¼Ê³¤¶È½ØÈ¡¶¨³¤×Ö·û´Ü

/// </summary>

/// <param name="str">Ô-×Ö·û´®</param>

/// <param name="length">Òª½ØÈ¡µÄ³¤¶È</param>

/// <returns>stringÐÍ×Ö·û´®</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(iCount>iCutLength)

{

int iLength=0;

C#截取定长字符串函数

for(int i=0;i<sInString.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(iLength>iCutLength)

{

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;

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]);

}

C#截取定长字符串函数

else

{

break;

}

}

if (sb.ToString() != stringToSub)

{

sb.Append("...");

}

return sb.ToString();

}

//·½·¨Ò»£º

/// <summary>

/// C#½ØÈ¡¶¨³¤×Ö·û´®º¯Êý

/// </summary>

/// <param name="string1">Ô-×Ö·û´®</param>

/// <param name="Len1">³¤¶È£¨Ô-×Ö·û´®ÖÐÈ«ÊǺº×ÖʱµÄºº×Ö¸öÊý£©</param>

/// <returns>½ØÈ¡ºóµÄ×Ö·û´®£¨ReCutStr£©</returns>

public static string CutStr(string string1, int Len1) {

string ReCutstr = string.Empty;

bool Remark = false;//Èç¹û²»¹»³¤¶È£¬Îª¼Ù

int n = string1.Length;

int TrueLen = 0;

int Pcount = 0; //Ó¢ÎÄ×Ö¼ÆÊý

int Gcount = 0; //ÖÐÎÄ×Ö¼ÆÊý

int Lcount = 0; //³¤¶È¼ÆÊý

char[] bytes = string1.ToCharArray();

foreach (char chrA in bytes)

{

if (Convert.ToInt32(chrA) >= 0 && Convert.ToInt32(chrA) <= 255)

{

Pcount = Pcount + 1;

}

else //Èç¹ûÊÇÖÐÎÄ

{

Gcount = Gcount + 1;

}

Lcount = (Pcount / 2) + Gcount;

C#截取定长字符串函数

TrueLen = Pcount + Gcount;

if (Lcount > Len1)//Èç¹û³¤¶ÈÒѹ»

{

ReCutstr = string1.Substring(0, TrueLen) + "..."; Remark = true;

break;

}

}

if (!Remark)//Èç¹ûÊDz»¹»³¤¶È

{

ReCutstr = string1;

}

return ReCutstr;

}

//·½·¨¶þ£º

public static string CutStr(string input,int count)

{

int tempcount = count;

if (input == null)

{

return string.Empty;

}

if (input.Length > count)

{

try

{

int lencount = 0;

int.TryParse(ContainsNumeral(input,

count).ToString(), out lncount);

count = lencount + count;

if (input.Length > count)

{

return input.Substring(0, count) + "..."; }

else

{

return input;

}

}

C#截取定长字符串函数

catch

{

return input.Substring(0, tempcount) + "..."; }

}

return input;

}

/// <summary>

/// ¹ýÂ˵Ä×Ö·û´®ÖдæÔÚÊý×Öʱ£¬¡°Á½¸öÊý×ÖÔøÒ»¸ö×Ö¡±

/// </summary>

/// <param name="input"></param>

/// <param name="count"></param>

/// <returns></returns>

public static double ContainsNumeral(string input, int count) {

string linput = input.Substring(0, count);

double lcount = 0;

int i = 0;

//1.¼òµ¥ÅжÏÊý×Ö

for (int k = 0; k < 10; k++)

{

if (linput.Contains(k.ToString()))

{

lcount += 0.5;

i++;

}

}

////2.È«ÃæÅжÏÊý×Ö

//for (int j = 0; j < linput.Length; j++)

//{

// for (int k = 0; k < 10; k++)

// {

//

1).Contains(k.ToString()))

// {

// lcount += 0.5;

// i++;

// break;

// }

// }

//}

if (linput.Substring(j,

C#截取定长字符串函数

if (i % 2 != 0) {

lcount += 0.5; } return lcount; }

本文来源:https://www.bwwdw.com/article/b4im.html

Top