Java 中 String 的常用方法(二)
本文介绍剩下的一些常用的 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 的常用方法(二)的更多相关文章
- java中String的常用方法
java中String的常用方法1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len= ...
- Java中String的常用方法总结
Java中String的常用方法总结 1.length()字符串的长度 String str="HelloWord"; System.out.println(str.length( ...
- JAVA中String类常用方法 I
String类常用方法有: int length() -– 返回当前字符串的长度 int indexOf(int ch) -– 查找ch字符在该字符串中第一次出现的位置 int indexOf(Str ...
- Java 中 String 的常用方法(一)
上一篇介绍了 String 中的几个常用构造方法,由 String 这个核心对象发散出去关于字符的编码,字符的字节表达,对 GC 的影响,正则表达式,模式匹配,这可能是 Java 里内涵最丰富的对象了 ...
- Java中String类常用方法(字符串中的子字符串的个数)
重点内容 4种方法: 1.int indexOf(String str)返回第一次出现的指定子字符串在此字符串中的索引. 2.int indexOf(String str, int startInde ...
- Java 中String常用方法
java中String的常用方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len ...
- Java中String常用方法
java中String的常用方法1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len= ...
- java基础——String的常用方法
java中String的常用方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); i nt le ...
- Java中String类的方法及说明
String : 字符串类型 一. String sc_sub = new String(c,3,2); // String sb_copy = new String(sb) ...
随机推荐
- Android-隐式意图激活操作系统通话界面
阅读Android操作系统的 packages/apps/phone/AndroidManifest.xml,是如何暴露的 AndroidManifest.xml Android操作系统在这里明确 ...
- task4: 结对编程-词频统计[修改版]
问题描述: 读取一个文件,统计其中单词出现次数,并按从高到低的顺序显示,相同顺序的字典序排列. 思路: 基于上次的程序用正则提取出文本里的单词,然后利用字典计数(先get,为null则置1,不为nul ...
- Spring AOP 简单入门笔记 (转)
分享一个自己写的最为简单的Spring AOP的应用,其实,本人也是学习Spring不久,只是把一些个人的理解分享下,供参考.可能很多人刚开始不太理解到底啥是AOP,其实它也是相对 OOP来说的,类似 ...
- Hibernate4获取Connection,ResultSet对象
项目中需要一个json对象,封装的时候,需要数据的列名. 在jdbc里面,可以有个ResultMetaData对象获取列名字.因为我用的是hibernate,这个框架已经封装了很多,一般是难以获得re ...
- .NET Core调用WCF的最佳实践
现在.NET Core貌似很火,与其他.NET开发者交流不说上几句.NET Core都感觉自己落伍了一样.但是冷静背后我们要也看到.NET Core目前还有太多不足,别的不多说,与自家的服务框架WCF ...
- autofac JSON文件配置
autofac是比较简单易用的IOC容器.下面我们展示如何通过json配置文件,来进行控制反转. 需要用到以下程序集.可以通过nugget分别安装 Microsoft.Extensions.Confi ...
- python网络编程--线程的方法,线程池
一.线程的其他方法(Thread其他属性和方法) ident() 获取线程id Thread实例对象的方法 isAlive() 设置线程名 getName() 返回线程名 setName() 设置线程 ...
- datatime模块
https://www.cnblogs.com/cindy-cindy/p/6720196.html
- long int double float
参考:https://blog.csdn.net/ideality_hunter/article/details/78432486 long是长整型,64位 int是短整型,32位 double是双精 ...
- Ubuntu下实现socks代理转http代理
代理(英语:Proxy),也称网络代理,是一种特殊的网络服务,允许一个网络终端(一般为客户端)通过这个服务与另一个网络终端(一般为服务器)进行非直接的连接.一些网关.路由器等网络设备具备网络代理功能. ...