//// <summary>
/// 桁数(byte)で文字列の一部を返す
/// </summary>
/// <param name="str">文字列</param>
/// <param name="limLength">桁数</param>
/// <returns>文字列</returns>
public static string Splite(string str, int limLength)
{
// 1. 文字列の桁数(BYTE単位)は引数桁数より小さい場合、リターンする。
int MaxLength = GetByteLength(str);
string strVal = "";
if (MaxLength <= limLength)
{
return str + "\r\n";
}
int len = 0;
int ii = 0;
byte Code;
// 2. 引数桁数の文字列のBYTE数を取得する
//* Unicodeに変換
byte[] byteInput = ( new UnicodeEncoding()).GetBytes( str );
for( ii = 0; (ii < byteInput.Length) && (len < limLength-1); ii++, len++ )
{
//* 半角英文字以外の場合
if( byteInput[++ii] != 0 )
{
//* 全角文字または半角カタカナの場合
if ( byteInput[ii] == 48 || byteInput[ii] == 255 )
{
Code = byteInput[ii-1];
//* 半角カタカナ以外の場合、2文字としてカウントする
if( !(( byteInput[ii] == 255 ) && ( Code >= 102 ) && ( Code <= 159)))
{
len++;
}
}
else
{
//* 漢字の場合
len++;
}
}
}
// 3. 文字列の一部をリターンする
while(GetByteLength(strVal)<=MaxLength)
{
int m = ii/2+1;
int n = ii/2;
string rtn = str.Substring(0, ii / 2 + 1);
if (GetByteLength(rtn)<=limLength)
{
strVal +=str.Substring(0, m)+"\r\n"+Splite(str.Substring(m,str.Length-m),limLength);
}
else
{
strVal +=str.Substring(0, n)+"\r\n"+Splite(str.Substring(n,str.Length-n),limLength);
}
}
return strVal;
}
/**//// <summary>
/// 文字列の桁数(BYTE単位)を取得する
/// </summary>
/// <param name="str">文字列</param>
/// <returns>桁数</returns>
public static int GetByteLength(string str)
{
int len = 0;
int ii = 0;
byte Code;
//* Unicodeに変換
byte[] byteInput = ( new UnicodeEncoding()).GetBytes( str );
for( ii = 0; ii < byteInput.Length; ii++, len++ )
{
//* 半角英文字以外の場合
if( byteInput[++ii] != 0 )
{
//* 全角文字または半角カタカナの場合
if ( byteInput[ii] == 48 || byteInput[ii] == 255 )
{
Code = byteInput[ii-1];
//* 半角カタカナ以外の場合、2文字としてカウントする
if( !(( byteInput[ii] == 255 ) && ( Code >= 102 ) && ( Code <= 159)))
{
len++;
}
}
//* 漢字の場合
else
{
len++;
}
}
}
return( len );
}
/// <summary>
/// 对textBox中的每行的长度作判断
/// </summary>
/// <param name="_textBox"></param>
/// <param name="limitLength"></param>
/// <returns></returns>
public string GetSplitString(TextBox _textBox,int limitLength)
{
string strVal = "";
for(int i = 0; i<_textBox.Lines.Length;i++)
{
strVal+=Splite(_textBox.Lines[i].ToString(),limitLength);
}
return strVal;
}