本文介绍剩下的一些常用的 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。

Java 中 String 的常用方法(二)的更多相关文章

  1. java中String的常用方法

    java中String的常用方法1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len= ...

  2. Java中String的常用方法总结

    Java中String的常用方法总结 1.length()字符串的长度 String str="HelloWord"; System.out.println(str.length( ...

  3. JAVA中String类常用方法 I

    String类常用方法有: int length() -– 返回当前字符串的长度 int indexOf(int ch) -– 查找ch字符在该字符串中第一次出现的位置 int indexOf(Str ...

  4. Java 中 String 的常用方法(一)

    上一篇介绍了 String 中的几个常用构造方法,由 String 这个核心对象发散出去关于字符的编码,字符的字节表达,对 GC 的影响,正则表达式,模式匹配,这可能是 Java 里内涵最丰富的对象了 ...

  5. Java中String类常用方法(字符串中的子字符串的个数)

    重点内容 4种方法: 1.int indexOf(String str)返回第一次出现的指定子字符串在此字符串中的索引. 2.int indexOf(String str, int startInde ...

  6. Java 中String常用方法

    java中String的常用方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len ...

  7. Java中String常用方法

    java中String的常用方法1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len= ...

  8. java基础——String的常用方法

    java中String的常用方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); i nt le ...

  9. Java中String类的方法及说明

    String : 字符串类型 一.      String sc_sub = new String(c,3,2);    //      String sb_copy = new String(sb) ...

随机推荐

  1. CentOS6.3安装MySQL5.5

    1.查看系统是否安装了MySQL 使用命令: #rpm -qa | grep mysql 2.卸载已安装的MySQL 卸载mysql命令如下: #rpm -e --nodeps  mysql-libs ...

  2. TSQL--集合处理

    UNION ALL 返回两个结果集中所有的行,返回结果集中会存在重复行 UNION 返回两个结果集中去重的行,返回结果集中无重复行 INTERSECT 返回两个结果集都有的行,返回结果集中无重复行 E ...

  3. 使用sn.exe为程序集签名

    前言 在写上一篇随笔时,为理解EF事务底层的原理,我去Github上把EF的源码下载放到自己项目调试,不过在编译时遇到了下面这个报错信息.经过一番查阅,了解到了程序集签名(也称强名称签名)的概念.报错 ...

  4. mysql--mysql的安装与目录介绍

    一.mysql的下载安装 1.下载安装 1.windows10下安装 我们采用绿色免安装版, 打开你的mysql文件夹中的bin目录,我的是这个样子的 将这个路径添加入系统环境变量,首先右键此电脑-- ...

  5. java学习笔记DOM4J解析(7)

    DOM4J即Document Object Model for Java使用java技术以文档方式解析XML数据的模型. DOM4J是开源组织提供的一个免费的.强大的XML解析工具,如果开发者需要在项 ...

  6. 【OCP认证12c题库】CUUG 071题库考试原题及答案(25)

    25. choose the best answer Evaluate the following SQL statement: ALTER TABLE hr.emp SET UNUSED (mgr_ ...

  7. 洛谷P5279 [ZJOI2019]麻将(乱搞+概率期望)

    题面 传送门 题解 看着题解里一堆巨巨熟练地用着专业用语本萌新表示啥都看不懂啊--顺便\(orz\)余奶奶 我们先考虑给你一堆牌,如何判断能否胡牌 我们按花色大小排序,设\(dp_{0/1,i,j,k ...

  8. Vmware下Kali设置桥接网络无法上网

    1.检查是否设置桥接 2.编辑>首选项>虚拟网络编辑器>选对本机上网的网卡 3.检查上网的网卡>适配器属性栏有没有 Vmware Bridge Protocol 桥接的服务. ...

  9. Word2Vec原理及代码

    一.Word2Vec简介 Word2Vec 是 Google 于 2013 年开源推出的一款将词表征为实数值向量的高效工具,采用的模型有CBOW(Continuous Bag-Of-Words,连续的 ...

  10. leetcode-383-Ransom Note(以空间换时间)

    题目描述: Given an arbitrary ransom note string and another string containing letters from all the magaz ...