C#实现根据字节数截取字符串并加上省略号的方法
作者:shichen2014 时间:2021-10-24 22:24:17
本文实例讲述了C#按字节数截取字符串并在后面加上省略号...的方法,这是一个自定义的C#函数,函数的使用说明如下:
<param name="origStr">原始字符串</param>
<param name="endIndex">提取前endIdex个字节</param>
<returns></returns>
函数代码如下:
public static string GetSubString(string origStr, int endIndex)
{
if (origStr == null || origStr.Length == 0 || endIndex < 0)
return "";
int bytesCount = System.Text.Encoding.GetEncoding("gb2312").GetByteCount(origStr);
if (bytesCount > endIndex)
{
int readyLength = 0;
int byteLength;
for (int i = 0; i < origStr.Length; i++)
{
byteLength = System.Text.Encoding.GetEncoding("gb2312").GetByteCount(new char[] { origStr[i] });
readyLength += byteLength;
if (readyLength == endIndex)
{
origStr = origStr.Substring(0, i + 1) + "...";
break;
}
else if (readyLength > endIndex)
{
origStr = origStr.Substring(0, i) + "...";
break;
}
}
}
return origStr;
}
以下所示示例也是根据字节数截取字符串的,只是这个函数后面不加省略号……
/// 按字节数截取字符串(不带省略号)
/// </summary>
/// <param name="origStr">原始字符串</param>
/// <param name="endIndex">提取前endIdex个字节</param>
/// <returns></returns>
public static string GetSub1String(string origStr, int endIndex)
{
if (origStr == null || origStr.Length == 0 || endIndex < 0)
return "";
int bytesCount = System.Text.Encoding.GetEncoding("gb2312").GetByteCount(origStr);
if (bytesCount > endIndex)
{
int readyLength = 0;
int byteLength;
for (int i = 0; i < origStr.Length; i++)
{
byteLength = System.Text.Encoding.GetEncoding("gb2312").GetByteCount(new char[] { origStr[i] });
readyLength += byteLength;
if (readyLength == endIndex)
{
origStr = origStr.Substring(0, i + 1);
break;
}
else if (readyLength > endIndex)
{
origStr = origStr.Substring(0, i);
break;
}
}
}
return origStr;
}
标签:C#,截取,字符串,方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot项目添加外部Jar包以及配置多数据源的完整步骤
2023-10-12 13:20:39
Java移动文件夹及其所有子文件与子文件夹
2023-08-01 09:53:38
Hadoop之Mapreduce序列化
2022-05-08 10:18:19
![](https://img.aspxhome.com/file/2023/3/82303_0s.png)
SpringBoot实现MapperScan添加动态配置(占位符)
2023-11-26 05:08:06
SpringData如何通过@Query注解支持JPA语句和原生SQL语句
2022-08-26 22:07:29
Androd 勇闯高阶性能优化之布局优化篇
2023-01-22 12:12:15
![](https://img.aspxhome.com/file/2023/8/87278_0s.png)
Android实现界面左右滑动切换功能
2022-08-30 02:00:58
![](https://img.aspxhome.com/file/2023/5/96745_0s.gif)
C#双缓冲实现方法(可防止闪屏)
2021-07-07 18:15:33
Java实现把两个数组合并为一个的方法总结
2022-08-04 16:30:03
实例讲述Java IO文件复制
2023-01-30 17:25:32
微信小程序 springboot后台如何获取用户的openid
2023-01-13 17:07:42
Spring Boot中lombok的安装与使用详解
2021-08-26 14:50:38
![](https://img.aspxhome.com/file/2023/5/66055_0s.jpg)
带你重新认识MyBatis的foreach
2023-11-21 08:44:54
完美解决idea创建文件时,文件不分级展示的情况
2022-01-01 22:10:19
![](https://img.aspxhome.com/file/2023/9/62099_0s.jpg)
解决springboot启动失败的问题('hibernate.dialect' not set)
2023-11-09 03:21:21
基于Luhn算法的银行卡校验规则
2022-06-05 00:56:15
c#防止多次运行代码收集分享
2021-09-27 18:28:26
解决IDEA无法下载maven依赖的问题
2023-12-05 17:06:59
![](https://img.aspxhome.com/file/2023/3/88503_0s.png)
unity实现弧形移动 可角度自定
2022-12-16 22:38:19
![](https://img.aspxhome.com/file/2023/3/116453_0s.jpg)
Java深入讲解异常处理try catch的使用
2023-11-04 13:00:32
![](https://img.aspxhome.com/file/2023/1/58921_0s.png)