append、replace、replaceAll、indexof、lastIndexOf、substring的用法
1.append
StringBuffer buf = new StringBuffer("Hard ");
String aString = "Waxworks";
buf.append(aString,3,4);
结果:"Hard w"
buf.append(aString,3,4);
结果为:"Hard work"
append s_content.append("</description>").append("\n");
2.replace和replaceAll
replace fileurl = fileurl.replace("../", "");//去掉../
1>replace的参数是char和CharSequence,既可以支持字符替换,也可以支持字符串替换。
2>replaceall参数是regex, replacement,regex表示是正则表达式。
replace :
String src = new String("ab43a2c43d");
System.out.println(src.replace("3","f"));=>ab4f2c4fd.
System.out.println(src.replace('3','f'));=>ab4f2c4fd.
System.out.println(src.replaceAll("\\d","f"));=>abffafcffd.
System.out.println(src.replaceAll("a","f"));=>fb43fc23d.
System.out.println(src.replaceFirst("\\d,"f"));=>abf32c43d
System.out.println(src.replaceFirst("4","h"));=>abh32c43d.
1
2
3
4
5
6
7
8
9
10
11
12
13
java 替换文本内容中的html标签:
String str="<html><h1>sdfsdfsdf</h1><img src=\"1.png\"></img></html>测试<p>测试</p>";
String abc = str.replaceAll("<[a-zA-Z]+[1-9]?[^><]*>", "").replaceAll("</[a-zA-Z]+[1-9]?>", "");
System.out.println(abc);
1
2
3
结果:
sdfsdfsdf测试测试
1
如何将字符串中的”\”替换成”\”:
String msgIn;
String msgOut;
msgOut=msgIn.replaceAll(“\\”,”\\\\”);
原因:
‘\’在java中是一个转义字符,所以需要用两个代表一个。例如System.out.println( “\” ) ;只打印出一个”\”。但是’\’也是正则表达式中的转义字符(replaceAll 的参数就是正则表达式),需要用两个代表一个。所以:\\被java转换成\,\又被正则表达式转换成\。
同样
CODE: \\\\
Java: \\
Regex: \
将字符串中的’/’替换成’\’的几种方式:
msgOut= msgIn.replaceAll(“/”, “\\”);
msgOut= msgIn.replace(“/”, “\”);
msgOut= msgIn.replace(‘/’, ‘\’);
3.lastIndexOf和indexof
- indexOf 和 lastIndexOf 是什么?
- indexOf 和 lastIndexOf 都是索引文件
- indexOf 是查某个指定的字符串在字符串首次出现的位置(索引值) (也就是从前往后查)
- lastIndexOf 是从右向左查某个指定的字符串在字符串中最后一次出现的位置(也就是从后往前查)
- eg:

- 注意:
- 那么问题来了 两个不是一前一后相反方向还是查么?怎么他们两个返回的索引值相同呢?
- 因为:lastIndexOf()方法虽然是从后往前搜索,但返回的位置是从前开始数数和计算的,所以结果和indexOf()方法返回的相同
接下来在看一个例子:

- 这个时候两个返回的索引值就不同了
- 因为: 前面已经说了indexOf是从前向后查 而lastIndexOf是从后向前查 但是二者返回索引都是从前开始数数和计算的
- 总结:
- 当数组(字符串)中所要查询的数(字符串/字符)在字符串(数组)中只出现一次的时候 二者返回的索引值相同
- 当数组(字符串)中所要查询的数(字符串/字符)在字符串(数组)中出现两次及以上的时候
- indexOf 返回的是 valuesearch 第一次在数组(字符串)出现的位置(从左往右)
- lastIndexOf 返回的是 valuesearch 最后一次在数组(字符串)出现的位置(从左往右)《只不过查询的方向不同而已》
4.substring
beginIndex -- 起始索引(包括), 索引从 0 开始。
endIndex -- 结束索引(不包括)
案例获取文件的结尾名
fileurl = fileurl.replace("../", "");//去掉../
log.info("==== fileurl:"+fileurl); // upload/shjz_db/2019/04/28/dfs_1_01_99_1556436628795_shjz_db_y5v9y5l2J4q6.jpg
//获取文件扩展名
int index = field_file.get("fileurl").lastIndexOf(".");
log.info("==== index:"+index); //75
String result = field_file.get("fileurl").substring(index + 1);
log.info("==== result:"+result);//jpg
append、replace、replaceAll、indexof、lastIndexOf、substring的用法的更多相关文章
- js中substr,substring,indexOf,lastIndexOf等的用法
1.substrsubstr(start,length)表示从start位置开始,截取length长度的字符串. var src="images/off_1.png";alert( ...
- JAVA中字符串函数subString的用法小结
本篇文章主要是对JAVA中字符串函数subString的用法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 String str; str=str.substring(int begi ...
- 使用js的indexOf,lastIndexOf,slice三函数轻易得到url的服务器,路径和页名
js的indexOf,lastIndexOf,slice能帮我们在js字符串处理时少走一些弯路. 程序如下: var url="http://www.cnblogs.com/xiandeda ...
- IndexOf() LastIndexOf() Contains() StartsWith() EndsWith()方法比较
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- String中的Indexof,LastIndexOf, Indexofany,LastIndexOfAny 的区别
本文转载自 http://www.cnblogs.com/qinying/archive/2008/09/22/1295730.html 定位子串是指在一个字符串中寻找其中包含的子串或者某个字符.在S ...
- js中substring/substr和C#中Substring的用法
一:在js中截取字符串的方法有两个:substring和substr 1.方法: substring(int stringIndex,[int endIndex]) 截取从索引为stringIndex ...
- C#中substring ()的用法
C#中substring ()的用法:http://www.cnblogs.com/bluespace/archive/2007/12/11/782336.html
- IndexOf、LastIndexOf、Substring的用法
String.IndexOf String.IndexOf 方法 (Char, Int32, Int32)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置 ...
- IndexOf、LastIndexOf、Substring的用法及C# foreach 中获取索引index的方法
String.IndexOf String.IndexOf 方法 (Char, Int32, Int32)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置 ...
随机推荐
- git用远程库的内容覆盖本地
git fetch --all 下载远程的库的内容到本地,不做任何的合并(怎么合并可以自己选择) git reset --hard origin/master 撤销本地.暂存区.版本库(用远程服务器的 ...
- VC DLL方法的__declspec导入导出
https://msdn.microsoft.com/zh-cn/library/a90k134d.aspx https://msdn.microsoft.com/zh-cn/library/ms23 ...
- 粉红色织梦CMS企业模板
粉红色织梦CMS企业网站模板,粉红色,织梦CMS,织梦企业模板,CMS模板. 模板地址:http://www.huiyi8.com/sc/7247.html
- mybatis进行分页,使用limit
这里记录两个思路: 首先是写一个不能执行的代码. <select id="query" parameterType="map" resultType=&q ...
- listen 73
Give Time to Feel Less Time-Squeeze Meetings, calls, kids, dogs, errands, exercise—and all those ema ...
- storm源码剖析(3):topology启动过程
storm的topology启动过程是执行strom jar topology1.jar MAINCLASS ARG1 ARG2 鉴于前面已经分析了脚本的解析过程,现在重点分析topology1.ja ...
- 集训Day5
生活还得继续 bzoj3771 题面让我笑了很长时间 给出 n个物品,价值为别为Xi且各不相同,现在可以取1个.2个或3个,问每种价值和有几种情况? *顺序不同算一种 很傻逼的一个母函数+容斥,用A( ...
- COGS 2581 无聊的会议V2
传送 题目大意 给定一个长为\(n\)的序列,定义\(y\)在三元对\((x,y,z)\)中成为中心轴当且仅当同时满足:\(a_x = a_y = a_z,y-x=z-y,x<y<z\)对 ...
- Python之常用模块(一)
time & datatime 模块 random os sys shutil json & picle time & datetime 时间戳(1970年1月1日之后 ...
- go http 下载视频(TS码流文件)(推荐一个网站学习 go example)
视频 http下载代码 dn.go(注意:代码很ugly,没怎么花时间) 总体感觉特别简单,网上看了下 net/http ,io这2个库的使用, 几分钟就写完了,感觉cpp 在做工具这块 开发效率的 ...