C#中判断字节多少,以及按字节截取
来源:互联网 发布:af淘宝旗舰店 编辑:程序博客网 时间:2024/05/22 02:29
1、函数判断字节多少
string strinit=“要判断的字符串”;
string getStr=System.Text.Encoding.Default.GetBytes(strinit).Length;
2、按字节截取字符串长度 按100个字节算
/// <summary>
/// 截取指定字节长度的字符串
/// </summary>
/// <param name="str">原字符串</param>
/// <param name="len">截取字节长度</param>
/// <returns></returns>
public static string CutByteString(string str, int len)
{
string result = string.Empty;// 最终返回的结果
if (string.IsNullOrEmpty(str)) { return result; }
int byteLen = System.Text.Encoding.Default.GetByteCount(str);// 单字节字符长度
int charLen = str.Length;// 把字符平等对待时的字符串长度
int byteCount = 0;// 记录读取进度
int pos = 0;// 记录截取位置
if (byteLen > len)
{
for (int i = 0; i < charLen; i++)
{
if (Convert.ToInt32(str.ToCharArray()[i]) > 255)// 按中文字符计算加2
{ byteCount += 2; }
else// 按英文字符计算加1
{ byteCount += 1; }
if (byteCount > len)// 超出时只记下上一个有效位置
{
pos = i;
break;
}
else if (byteCount == len)// 记下当前位置
{
pos = i + 1;
break;
}
}
if (pos >= 0)
{ result = str.Substring(0, pos); }
}
else
{ result = str; }
return result;
}
/// <summary>
/// 截取指定字节长度的字符串
/// </summary>
/// <param name="str">原字符串</param>
/// <param name="startIndex">起始位置</param>
/// <param name="len">截取字节长度</param>
/// <returns></returns>
public static string CutByteString(string str, int startIndex, int len)
{
string result = string.Empty;// 最终返回的结果
if (string.IsNullOrEmpty(str)) { return result; }
int byteLen = System.Text.Encoding.Default.GetByteCount(str);// 单字节字符长度
int charLen = str.Length;// 把字符平等对待时的字符串长度
if (startIndex == 0)
{ return CutByteString(str, len); }
else if (startIndex >= byteLen)
{ return result; }
else //startIndex < byteLen
{
int AllLen = startIndex + len;
int byteCountStart = 0;// 记录读取进度
int byteCountEnd = 0;// 记录读取进度
int startpos = 0;// 记录截取位置
int endpos = 0;// 记录截取位置
for (int i = 0; i < charLen; i++)
{
if (Convert.ToInt32(str.ToCharArray()[i]) > 255)// 按中文字符计算加2
{ byteCountStart += 2; }
else// 按英文字符计算加1
{ byteCountStart += 1; }
if (byteCountStart > startIndex)// 超出时只记下上一个有效位置
{
startpos = i;
AllLen = startIndex + len - 1;
break;
}
else if (byteCountStart == startIndex)// 记下当前位置
{
startpos = i + 1;
break;
}
}
if (startIndex + len <= byteLen)//截取字符在总长以内
{
for (int i = 0; i < charLen; i++)
{
if (Convert.ToInt32(str.ToCharArray()[i]) > 255)// 按中文字符计算加2
{ byteCountEnd += 2; }
else// 按英文字符计算加1
{ byteCountEnd += 1; }
if (byteCountEnd > AllLen)// 超出时只记下上一个有效位置
{
endpos = i;
break;
}
else if (byteCountEnd == AllLen)// 记下当前位置
{
endpos = i + 1;
break;
}
}
endpos = endpos - startpos;
}
else if (startIndex + len > byteLen)//截取字符超出总长
{
endpos = charLen - startpos;
}
if (endpos >= 0)
{ result = str.Substring(startpos, endpos); }
}
return result;
}
- C#中判断字节多少,以及按字节截取
- C# 字符串按字节截取
- C#截取字符串按字节截取SubString
- C# 字节数组截取
- C# 按字节数截取string字符串
- java中按字节截取String字符串.
- JAVA中按字节截取字符串
- JAVA中按字节截取字符串
- js中按字节截取字符串
- iOS中按字节截取NSString
- 按字节截取字符串
- 按字节截取字符串
- 按字节截取字符串
- 按字节截取字符串
- 按字节截取字符串
- 按字节截取字符串
- 按字节截取字符串
- C#中实现截取固定长度字节数的字符串
- ajax实现验证用户名是否已经存在(struts2)
- 装饰者模式-有层次地组织代码
- 4.0,4.1系统手机 @android:color/transparent 显示黑色
- <a>标签的取消跳转和恢复
- JavaWeb: 报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- C#中判断字节多少,以及按字节截取
- mysql的sql执行计划详解(非常有用)
- Servlet处理get请求中文乱码问题
- 【ubuntu】windows与linux之间的远程登录
- VS2010 lib和dll导出路径设置(转)
- 浙江大学PAT_乙级_1040. 有几个PAT(25)
- 几个据说可以考验你知识范围的问题
- POJ 1011 Sticks
- 关于赛马的问题,25匹赛出前3名或者前5名