本文介绍剩下的一些常用的 String 中的方法。

1、replace 方法 、replaceFirst 方法和 replaceAll 方法

replace(char oldChar, char newChar)
Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.

replace(CharSequence target, CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

replaceFirst(String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement.

replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.

这几个方法都是用指定的字符或字符串将原有的字符串中的对应内容进行替换,前面两个不支持正则表达式,后边两个支持正则

String x = "[hello\\google\\bye]";
System.out.println(x.replace('\\', '/'));
System.out.println(x.replace("oo", "++"));
System.out.println(x.replace("\\", "++"));
System.out.println(x.replaceFirst("\\\\", "/"));
System.out.println(x.replaceAll("oo", "++"));
System.out.println(x.replaceAll("\\\\", "++"));

输出结果为:

[hello/google/bye]
[hello\g++gle\bye]
[hello++google++bye]
[hello/google\bye]
[hello\g++gle\bye]
[hello++google++bye]

根据测试 replaceAll 函数要更快一些。看源码发现,replace 函数里面仍使用 replaceAll 函数。

总体原则:当字符串无法确定是否具有转义字符时,而且也不需要转义时,建议使用 replace 函数;否则,使用 replaceAll 函数。

PS:正则表达式中匹配一个反斜杠为何需要用四个反斜杠?

分析一下字符串 "\\\\",第一个和第三个为转义符,第二个和第四个为斜杠本身。

字符串中表示斜杠就需要两个斜杠 "\\",

正则表达式里边的斜杠则需要转义,用 "\\" 表示,

所以我们先要表示正则表达式里边的斜杠 "\\",然后在用字符串表示出来,而这 2 个斜杠分别需要一个转义符,这样就成了 4 个斜杠在正则表达式中表示一个斜杠。

2、matches 方法

matches(String regex)
Tells whether or not this string matches the given regular expression.

该方法用来判断这个字符串是否匹配给定的正则表达式,符合则返回 true,反之则返回 false。

3、split 方法

split(String regex)
Splits this string around matches of the given regular expression.

split(String regex, int limit)
Splits this string around matches of the given regular expression.

字符串分割的方法,返回值为一个 String 类型的数组。

第一个参数 int limit 可以限制进行正则匹配的次数,

如果 limit > 0,则进行正则匹配的次数为 limit - 1 次,

如果 limit < 0,则不限制匹配的次数,

如果 limit = 0,则不限制匹配的次数,并且分隔之后的数组结尾的空字符串被忽略。

String str = "boo:and:foo";
System.out.println(Arrays.toString(str.split(":", 2)));
System.out.println(Arrays.toString(str.split(":", 5)));
System.out.println(Arrays.toString(str.split(":", -2)));
System.out.println(Arrays.toString(str.split("o", 5)));
System.out.println(Arrays.toString(str.split("o", -2)));
System.out.println(Arrays.toString(str.split("o", 0)));

输出结果为:

[boo, and:foo]
[boo, and, foo]
[boo, and, foo]
[b, , :and:f, , ]
[b, , :and:f, , ]
[b, , :and:f]

4、toLowerCase 方法和 toUpperCase 方法

toLowerCase()
Converts all of the characters in this String to lower case using the rules of the default locale.

toUpperCase()
Converts all of the characters in this String to upper case using the rules of the default locale.

大小写转换方法

String str = "hello google";
System.out.println(str.toUpperCase());
String str1 = "HELLO google";
System.out.println(str1.toLowerCase());

输出结果为:

HELLO GOOGLE
hello google

5、trim 方法

trim()
Returns a string whose value is this string, with any leading and trailing whitespace removed.

去掉给定字符串前后的空白

String str = "       hello, google              ";
System.out.println(str.trim());

输出结果:

hello, google

6、valueOf 方法

将其他类型的对象转换为字符串,包括了 boolean,char,char[],double,float,int,long 以及 Object 类型。

返回对应字符串或者 null。

最新文章

  1. nginx-(/usr/local/nginx/conf/nginx.conf)更改配置文件
  2. MAC OS UI设计
  3. linux命令学习使用记录
  4. 线程池大小 &amp; cpu core
  5. Hadoop学习笔记: 全排序
  6. 一个想了好几天的问题——关于8086cpu自己编写9号中断不能单步的问题
  7. Yii路径总结(转)
  8. 转:基于科大讯飞语音API语音识别开发详解
  9. ANDROID与.Net之间JSON实践
  10. css系列:input的placeholder在safari下设置行高失效
  11. javaCV开发详解之3:收流器实现,录制流媒体服务器的rtsp/rtmp视频文件(基于javaCV-FFMPEG)
  12. JavaScript的5中基本数据类型
  13. 安装eclipse时跳转到网页提示JRE Missing
  14. Linux进程管理(第二版) --计划任务
  15. 计算机网络一:OSI七层、TCP/IP五层与TCP/IP四层
  16. 【九天教您南方cass 9.1】01 安装Cad和Cass9.1
  17. 量化交易-外汇交易-MetaTrader5
  18. 【排序算法】选择排序(Selection sort)
  19. Java并发容器——CopyOnWriteArrayList
  20. win7下 go语言开发环境搭建(64bit)

热门文章

  1. 【转】Android EventBus初探
  2. Buffer Pool--内存总结2
  3. [uwp]MVVM模式实战之必应壁纸查看器
  4. JS-获取任意html节点属性
  5. Android Dialog 的一些特性
  6. Ubuntu16.04 - 怎么能够更好设置PATH变量,便于管理?
  7. “全栈2019”Java多线程第一章:认识多线程
  8. java中数组的插入
  9. 中山纪念中学培训DAY1
  10. LOJ#3084. 「GXOI / GZOI2019」宝牌一大堆(递推)