runxinzhi.com
首页
百度搜索
截取字符串函数
截取字符串函数:解决了中文与英文截取不同的问题。
Code
1
字符串截取函数
#region
字符串截取函数
2
public
static
string
CutString(
string
inputString,
int
len)
3
{
4
5
ASCIIEncoding ascii
=
new
ASCIIEncoding();
6
int
tempLen
=
0
;
7
string
tempString
=
""
;
8
byte
[] s
=
ascii.GetBytes(inputString);
9
for
(
int
i
=
0
; i
<
s.Length; i
++
)
10
{
11
if
((
int
)s[i]
==
63
)
12
{
13
tempLen
+=
2
;
14
}
15
else
16
{
17
tempLen
+=
1
;
18
}
19
20
try
21
{
22
tempString
+=
inputString.Substring(i,
1
);
23
}
24
catch
25
{
26
break
;
27
}
28
29
if
(tempLen
>
len)
30
break
;
31
}
32
byte
[] mybyte
=
System.Text.Encoding.Default.GetBytes(inputString);
33
if
(mybyte.Length
>
len)
34
tempString
+=
"
…
"
;
35
36
return
tempString;
37
}
38
#endregion
Repeat中调用该函数:
Code
1
<
asp:Repeater ID
=
"
Repeater1
"
runat
=
"
server
"
>
2
<
ItemTemplate
>
3
<
a href
=
'
<%# "Bulletin/Details.aspx?BulletinID="+ DataBinder.Eval(Container.DataItem,"ID") %>
'
><%
# Warning.CutString(DataBinder.Eval(Container.DataItem,
"
Title
"
).ToString(),
22
)
%></
a
>
4
</
ItemTemplate
>
5
</
asp:Repeater
>
去除最后那个“|”字符
Code
1
private
string
ClearLastChar(
string
str)
2
{
3
if
(str
==
null
)
4
{
5
return
str;
6
}
7
else
8
{
9
if
(str.Length
>
0
)
10
{
11
return
str.Substring(
0
, str.LastIndexOf(
"
|
"
));
12
}
13
else
14
{
15
return
""
;
16
}
17
18
}
19
}
20
相关阅读:
每个Java开发人员都应该知道的4个Spring注解
JVM中的动态语言支持简介
深入探索Java设计模式(五)之构建器模式
Java——MVC模式
程序出了问题,报错只能参考
查看电脑端口占用情况
Java——参数传递
Python——关于定义过程
Java——super的使用
关于如何查看论文是否被SCI或者EI收录
原文地址:https://www.cnblogs.com/zhangchenliang/p/1035703.html
最新文章
clojure配置
es查询命令备份(只需要网页9200/_plugin/head/就可以访问)
lucene和es总结
tf-idf知多少?
Application详解
Intent详解
中介者模式
模板方法模式
迭代器模式
观察者模式
热门文章
本地广播( LocalBroadcastManager)源码解析
发送广播(sendBroadcast)的过程源码分析
注册广播(registerReceiver)过程源码分析
使用JDK工具进行Java服务器应用程序故障排除
使用Redis和Java进行数据库缓存
使用Spring Boot和OAuth构建安全的SPA
带有外部Tomcat的Spring Boot
在Spring Boot中使用Docker在测试中进行高级功能测试
微服务架构中的Redis
在并发Java应用程序中检测可见性错误
Copyright © 2020-2023
润新知