zoukankan
html css js c++ java
字符串截取固定长度的方法(C#)
字符串截取固定长度的方法(C#)
这个函数也没有什么特别之处,就是可以截取一定长度的字符串,可能小特点就是len是字节,解决了汉字与英文字节不一样导致直接截取到的长度不一样的问题
1
字符串截取函数
#region
字符串截取函数
2
public
static
string
CutString(
string
inputString,
int
len)
3
{
4
5
6
ASCIIEncoding ascii
=
new
ASCIIEncoding();
7
int
tempLen
=
0
;
8
string
tempString
=
""
;
9
byte
[] s
=
ascii.GetBytes(inputString);
10
for
(
int
i
=
0
;i
<
s.Length;i
++
)
11
{
12
if
((
int
)s[i]
==
63
)
13
{
14
tempLen
+=
2
;
15
}
16
else
17
{
18
tempLen
+=
1
;
19
}
20
21
try
22
{
23
tempString
+=
inputString.Substring(i,
1
);
24
}
25
catch
26
{
27
break
;
28
}
29
30
if
(tempLen
>
len)
31
break
;
32
}
33
//
如果截过则加上半个省略号
34
byte
[] mybyte
=
System.Text.Encoding.Default.GetBytes(inputString);
35
if
(mybyte.Length
>
len)
36
tempString
+=
"
…
"
;
37
38
39
return
tempString;
40
}
41
#endregion
42
查看全文
相关阅读:
Java代码检测工具
java编程工具
100-days: The one day
前端书籍整理
vue 学习笔记(二)
从零开始写一个npm包及上传
Vue Baidu Map 插件的使用
对数组对象进行过滤
使用css方法使footer保持在页面的最底部
判断是第一次打开界面?还是刷新
原文地址:https://www.cnblogs.com/xiaozhang/p/1051884.html
最新文章
【构造】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) A. Bear and Different Names
【组合数】【乘法逆元】 Codeforces Round #404 (Div. 2) D. Anton and School
ACM-ICPC 2016亚洲区域赛(沈阳站)游记(滚粗记)
【二分】Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale
【带权并查集】Gym
【带权并查集】【离散化】vijos P1112 小胖的奇偶
【带权并查集】poj1182 食物链
Java数据类型的转换
CSS布局技巧之--各种居中
CSS布局技巧之-宽度自适应
热门文章
eclipse maven 安装 和 配置
常用进制转换
常见的编码与字符集
普通计数单位与计算机计数单位区别及时间计数单位
IT英语4-计算机英语缩写术语
IT英语4-其他常用计算机英语2
IT英语3-其他常用计算机英语1
IT英语2-编程词汇编程英语词汇
IT英语1-计算机算法常用术语中英对照
IT冷笑话
Copyright © 2011-2022 走看看