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
查看全文
相关阅读:
Git常用命令
Shell脚本学习
Shell脚本学习
Shell脚本学习
Git ignore文件的用法
RSA非对称加密算法
C++ 标准库中的堆(heap)
EM(Entity FrameWork)- code first , using in Visual stdio 2017
C# 图片文字识别
C# 调 C++ DLL 托管代码中释放非托管函数分配的内存
原文地址:https://www.cnblogs.com/xiaozhang/p/1051884.html
最新文章
01x01 MarkDown语法
01x02 常用Dos命令
C++基础(一)
数据结构之图
VMware15 如何安装苹果?
sqlmap的使用
How to enforce Jenkins to use TLS 1.2
com.sun.mail.smtp.SMTPAddressFailedException: 454 4.7.1
git常用命令
jenkins job起停远程slave机器上的系统服务
热门文章
邮件服务器postfix,saslauthd,dovecot安装配置提供SMTP服务
为Jenkins添加slave node
pom.xml 转化为 ivy.xml
gradle 处理 properties file
从reps下载jar,然后unzip test jar,再执行junit测试
常用Linux及awk命令学习
Appium配置及踩坑
Jmeter选项含义
jQuery AJAX相关方法
jQuery语法
Copyright © 2011-2022 走看看