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的用法的更多相关文章

  1. js中substr,substring,indexOf,lastIndexOf等的用法

    1.substrsubstr(start,length)表示从start位置开始,截取length长度的字符串. var src="images/off_1.png";alert( ...

  2. JAVA中字符串函数subString的用法小结

    本篇文章主要是对JAVA中字符串函数subString的用法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 String str; str=str.substring(int begi ...

  3. 使用js的indexOf,lastIndexOf,slice三函数轻易得到url的服务器,路径和页名

    js的indexOf,lastIndexOf,slice能帮我们在js字符串处理时少走一些弯路. 程序如下: var url="http://www.cnblogs.com/xiandeda ...

  4. IndexOf() LastIndexOf() Contains() StartsWith() EndsWith()方法比较

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  5. String中的Indexof,LastIndexOf, Indexofany,LastIndexOfAny 的区别

    本文转载自 http://www.cnblogs.com/qinying/archive/2008/09/22/1295730.html 定位子串是指在一个字符串中寻找其中包含的子串或者某个字符.在S ...

  6. js中substring/substr和C#中Substring的用法

    一:在js中截取字符串的方法有两个:substring和substr 1.方法: substring(int stringIndex,[int endIndex]) 截取从索引为stringIndex ...

  7. C#中substring ()的用法

    C#中substring ()的用法:http://www.cnblogs.com/bluespace/archive/2007/12/11/782336.html

  8. IndexOf、LastIndexOf、Substring的用法

     String.IndexOf String.IndexOf 方法 (Char, Int32, Int32)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置 ...

  9. IndexOf、LastIndexOf、Substring的用法及C# foreach 中获取索引index的方法

     String.IndexOf String.IndexOf 方法 (Char, Int32, Int32)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置 ...

随机推荐

  1. AnkhSVN

    安装和配置 签入签出问题 1.安装和配置 ①安装.(貌似默认的安装到C:\Program Files\AnkhSVN 2下,开始菜单也没快捷?) ②源代码管理器设置:打开vs2012,工具→选项→源代 ...

  2. redhat5.1上安装oracle 10.2g客户端及配置使用

    一)安装 1.命令 rpm -q gcc make binutils setarch compat-db compat-gcc compat-gcc-c++ compat-libstdc++ comp ...

  3. 9.2 NOIP提高组试题精解(2)

    9-18 fruit.c #include <stdio.h> #define MAXN 10000 int Queue1[MAXN], Queue2[MAXN]; void Insert ...

  4. centos 7 部署 mysql 报错记录

    1. Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY 这是由于yum安装了旧版本的GPG keys造成的,解决办法就是 引用  rpm --i ...

  5. PYTHON 爬虫笔记十一:Scrapy框架的基本使用

    Scrapy框架详解及其基本使用 scrapy框架原理 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了 ...

  6. matlab之find()函数

    Find 这个函数用处也挺大的,这几天看很多程序都见到这一函数,今天要好好给阐述,了解下这个函数是为了找到矩阵或者是数组,向量中的非零元素.下面一大段英文没耐心看.看看例子就行了. 第一个用法是 nd ...

  7. 时尚设计div+css免费模板

    时尚设计div+css免费网页模板,时尚设计,div+css. http://www.huiyi8.com/moban/

  8. JavaScript--Object类

    Object类是所有JavaScript类的基类,提供了一种创建自定义对象的简单方式,不需要程序员再定义构造函数. 主要属性: constructor-对象的构造函数 prototype-获得类的pr ...

  9. SFTP 文件上传下载引用代码

    http://sha1064616837.iteye.com/blog/2036996 http://www.cnblogs.com/itmanxgl/p/fe5d33512609fe540eb08a ...

  10. Go丨语言对MySQL数据库的增、删、改、查操作

    1.建立数据库名为: go_test_db 2.建表名为:userinfo 字段: uid int username varchar language varchar created varchar ...