java.lang.String中的replace方法到底替换了一个还是全部替换了。
你没有看错我说的就是那个最常用的java.lang.String,String可以说在Java中使用量最广泛的类了。
但是我却发现我弄错了他的一个API(也可以说是两个API),这个API是关于字符串替换的。
我的错误见解
之前我一直以为String有个API是这样子的,String replace(String oldString, String newString)
用来替换String中的第一个oldString为newString,这可能和我之前做的东西基本山替换的都是单一的字符串有关吧。
但是当我看到队友写的代码int containStringNumber = string.length() - string.replace("containString", "").length()
,我认为containStringNumber的值是0或者1,但是我错了。这个结果可能会大于1的。
实际情况
通过API文档可以看出来String有4个替换方法:
1. String replace(char oldChar, char newChar)
描述:Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.
谷歌翻译:返回使用newChar替换此字符串中所有出现的oldChar而产生的字符串。
2. String replace(CharSequence target, CharSequence replacement)
描述:Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
谷歌翻译: 将与该文字目标序列匹配的此字符串的每个子字符串替换为指定的文字替换序列。
3. String replaceAll(String regex, String replacement)
描述:Replaces each substring of this string that matches the given regular expression with the given replacement.
谷歌翻译:将给定替换的给定正则表达式匹配的此字符串的每个子字符串替换。
4. String replaceFirst(String regex, String replacement)
描述:Replaces the first substring of this string that matches the given regular expression with the given replacement.
谷歌翻译:将给定替换的给定正则表达式匹配的此字符串的第一个子字符串替换。
是的,String里面根本没有跟那个我自以为是的方法。
第一个方法是替换第一个没错,但是替换的是一个char,不是String。
第二个方法是替换的CharSequence(包括String, StringBuffer, StringBuilder),但是替换的是全部。
第三个replaceAll是替换全部的字符串正则表达式,
第四个是缺实是替换了第一个,但是人家名字写得明明白白的replaceFirst,而且替换的也是正则表达式。
教训与总结
这让我想起来前天的一篇文章《On The Value Of Fundamentals In Software Development 》,英文不好的可以自己翻译下。
我要好好学习Java的API了,白干了四年了,纯属一级菜鸟啊。
java.lang.String中的replace方法到底替换了一个还是全部替换了。的更多相关文章
- java.lang.String中的trim()方法的详细说明(转)
String.Trim()方法到底为我们做了什么,仅仅是去除字符串两端的空格吗? 一直以为Trim()方法就是把字符串两端的空格字符给删去,其实我错了,而且错的比较离谱. 首先我直接反编译String ...
- java.lang.String 类的所有方法
java.lang.String 类的所有方法 方法摘要 char charAt(int index) 返回指定索引处的 char 值. int codePointAt(int index) 返回指定 ...
- java:常用类(包装类,equals和==的比较,Date,java.lang.String中常用方法,枚举enum)
*包装类: 将基本类型封装成类,其中包含属性和方法以方便对象操作. *byte---->Byte *short--->Short *long--->Long *float---> ...
- java.lang.Math中的基本方法
java.lang.Math类提供的方法都是static的,“静态引入 ”使得不必每次在调用类方法时都在方法前写上类名: import static java.lang.Mat ...
- mybatis invalid comparison: java.sql.Timestamp and java.lang.String报错解决方法
这个错的意思是:java.sql.Timestamp和java.lang.String无效的比较 错误的原因是:拿传入的时间类型参数与空字符串进行比较就会报这个异常 解决方法:只保留非null判断就可 ...
- java基础---->String中的split方法的原理
这里面主要介绍一下关于String类中的split方法的使用以及原理. split函数的说明 split函数java docs的说明: When there is a positive-width m ...
- Java的String中的subString()方法
方法如下: public String substring(int beginIndex, int endIndex) 第一个int为开始的索引,对应String数字中的开始位置, 第二个是截止的索引 ...
- java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String错误的解决方法
mmobjectid是在Oracle数据库中对应的是Number类型的,在JavaBean中定义的是Long类型的. List<BigDecimal> mmobjidAllFromMars ...
- java.lang.String里面的trim()方法——删除首尾空格
结果如图 package com.softeasy.test1; public class String_trim { public static void main(String[] args) { ...
随机推荐
- poj-1021--2D-Nim--点阵图同构
2D-Nim Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4136 Accepted: 1882 Descriptio ...
- CSS-使整个页面上的全部元素可编辑
# [在线预览](https://jsfiddle.net/1010543618/6zu1gush/) ## 方法一 - 使用 html 的 contenteditable 属性: [HTML 5 全 ...
- 使用Android Studio打出apk包
参考: Android Studio 超级简单的打包生成apk https://blog.csdn.net/hefeng6500/article/details/79869647 为什么要打包: ap ...
- yarn 国内加速,修改镜像源
为什么慢 由于默认情况下执行 yarn 各种命令是去国外的 yarn 官方镜像源获取需要安装的具体软件信息,所以在不使用代理.不翻墙的情况下,从国内访问国外服务器的速度相对比较慢 可以通过以下命令快速 ...
- 关系型数据库MySQL(一)_增删改查
1.创建表单 create table product (product_id char(4) not null, product_name varchar(100) not null, sa ...
- python 简易计算器
import tkinter import tkinter.messagebox import math ''' 波波版计算器可实现的功能 1.能进行简单的加减惩处 2.能进行开根号操作 3.能进行后 ...
- MSF——基本使用和Exploit模块(一)
MSF系列: MSF——基本使用和Exploit模块(一) MSF——Payload模块(二) MSF——Meterpreter(三) MSF——信息收集(四) MSF——Metasploit Fra ...
- CSS深入理解line-height
1.line-height高度基于基线 2. 3.p元素的高度由行高决定的 4. 5.
- java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer;
java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L ...
- POJ:3371 Connect the Cities(最小生成树)
http://acm.hdu.edu.cn/showproblem.php?pid=3371 AC代码: /** /*@author Victor /* C++ */ #include <bit ...