C#截取字符串按字节截取SubString
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;
}
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;
}
调用:
private void button1_Click(object sender,EventArgs e)
{
string s="一二3456七八";
s=CutByteString(s,5);
MessageBox.Show(s); //输出 “一二3”
s=CutByteString(s,3,5);
MessageBox.Show(s); //输出 “二345”
}
C#截取字符串按字节截取SubString的更多相关文章
- 分割字符串和截取字符串:split 和substring
//按“,”截取字符串 String id="123123,234534,453456"; String[] idArry = id.trim().split(",&qu ...
- SQL使用UPDATE和SUBSTRING截取字符串方法,从头截取到某个位置,截取中间片段,字符串中间截取到末尾或删除前面的字符串
//从头截取 update 表名 set 表列名 =SUBSTRING(表列名,1,目标位置数值) //!计数从1开始,从左往右 where 条件 //条件自己选择,不加where条件会更新所有 ...
- JS 字符串转字节截取
/* * param str 要截取的字符串 * param L 要截取的字节长度,注意是字节不是字符,一个汉字两个字节 * return 截取后的字符串 */ function CutStr(str ...
- JavaScript截取字符串的Slice、Substring、Substr函数简单比较还有indexof函数应用
//截取字符,一看就明白!!! var str = "0123456789"; alert(str.substring(5)); 弹出 //56789 alert(str.subs ...
- 解决在C#(.net)按字节数截取字符串最后出现乱码的问题
最近需要用到按字节数截取字符串.在网上找了很多方法. Encoding.Default.GetString采用的DefaultEncoding.UTF8.GetBytes采用的是utf-8编码.这样当 ...
- 【转】C#中如何实现左截取和右截取字符串
使用C#语法编写程序时,我们需要截取一个字符串左边或右边的若干个字符,该如何操作呢?在VB中可以使用left或right函数实现,C#中没有提供这样的函数呢?答案是没有.但是,C#中提供Substri ...
- mysql根据字符截取字符串(总结)
mysql根据字符截取字符串(总结) 1.1 前言 为结合自己平常查资料的习惯,我会先给出例子,然后再对相关知识进行详解.该案例使用到的函数为:SUBSTRING_INDEX 1.2 需要实现的实 ...
- js查找字符串、js截取
js查找元素.js查找字符串 let index=data.indexOf(","); js截取.js截取字符串 $("#bankurl_id").val(da ...
- Linux Shell 截取字符串
Linux Shell 截取字符串 shell中截取字符串的方法很多 ${var#*/} ${var##*/} ${var%/*} ${var%%/*} ${var:start:len} ${var: ...
随机推荐
- Spring+SpringMVC+mybatis框架整合
1.jdbc.properties 1 driverClassName=com.mysql.jdbc.Driver 2 url=jdbc\:mysql\://127.0.0.1\:3306/slsal ...
- 什么是 PWA?
出处:https://segmentfault.com/a/1190000012353473
- php+js实现重定向跳转并post传参
页面重定向跳转并post传参 $mdata=json_encode($mdata);//如果是字符串无需使用json echo " <form style='display:none; ...
- LOJ 2585 「APIO2018」新家 ——线段树分治+二分答案
题目:https://loj.ac/problem/2585 算答案的时候要二分! 这样的话,就是对于询问位置 x ,二分出一个最小的 mid 使得 [ x-mid , x+mid ] 里包含所有种类 ...
- Excel文件转为其他格式文件
引用:Spire.XLS 是一个 Excel 文件的读写库,无需安装office,使用起来也挺方便的.Spire还有一些其他的库(Spire.Doc,Spire.Pdf……) 说明:Spire.XLS ...
- eclipse卡死在search for main types 20 files to index
run as application时,提示search for main types 20 files to index (*/*/*.jar)某个maven依赖jar出了问题,找不到main方法 ...
- SDI视频采集过程
SDI视频采集过程 GTP收发模块为视频采集系统的核心部分,包含发送和接收,完成对信号的解串和串码.并且HD-SDI信号中并非所有的信号都是有效视频信号,这部分功能由数据分析模块实现,并将提取出来的有 ...
- linux在线中文手册
linux在线中文手册 http://linux.51yip.com/ 百度中的百度应用也不错 http://www.baidu.com/s?word=linux%E5%91%BD%E4%BB%A4& ...
- Ubuntu PPA软件源
PPA,其英文全称为 Personal Package Archives,即个人软件包档案.是 Ubuntu Launchpad 网站提供的一项源服务,允许个人用户上传软件源代码,通过 Launchp ...
- Mysql 性能优化4 mysql参数配置
mysql 参数的介绍 大概450项参数,大多保持默认就可以了 错误的参数 崩溃,错误,运行缓慢. 参数最好在生产环境前配置好.最好不要在生产环境 中 直接配置,有可能不会立即生效,或者之前的数据和配 ...