java replace会替换吗,java replace replaceAll 替换字符串的用法和区别实例

news/2024/10/22 12:28:25/

java replace replaceAll 是替换字符串最常用的方法,但实际上用法是有区别的,replace只能传字符不能传正则表达式,replaceAll 默认传入的就是正则表达式。下面是实例测试代码:

public class TestDemo {

public static void main(String[] args) {

//需求:实现在-x.txt替换成-9x.txt

String filename = “F:/kanbox/2014091-4.txt”;

String newname = filename.replaceAll(“-([0-9])”, “-9$1″);

System.out.println(newname);//F:/kanbox/2014091-94.txt

String newname2 = filename.replace(“-([0-9])”, “-9$1″);

System.out.println(newname2);//F:/kanbox/2014091-4.txt,没达到预期效果

String newname3 = filename.replace(“-”, “-9″);

System.out.println(newname3);//F:/kanbox/2014091-94.txt

}

}

扩展知识,api里面的说明:

String java.lang.String.replaceAll(String regex, String replacement)

Replaces each substring of this string that matches the given regular expression with the given replacement.

An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression

————————————-

String java.lang.String.replace(CharSequence target, CharSequence replacement)

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing “aa” with “b” in the string “aaa” will result in “ba” rather than “ab”.

Parameters:

target The sequence of char values to be replaced

replacement The replacement sequence of char values

历史上的今天:

9bd101509341196819122f36086c9a60.png


http://www.ppmy.cn/news/545799.html

相关文章

Python学习笔记之常用操作符,条件分支和循环用法示例

本文实例讲述了Python常用操作符,条件分支和循环用法。分享给大家供大家参考,具体如下: #Pyhon常用操作符想要学习Python?Python学习交流群:973783996满足你的需求,资料都已经上传群文件,可以自行下载&…

新装mac系统finder不断重启

自从安装了新系统小牛,我被烦人finder重启问题折磨得不能正常工作了。终于找到解决方法。罪魁祸首就是kanbox,酷盘。卸载了以后一切ok。 sudo mv /Library/Application\ Support/KanBox /Library/Application\ Support/KanBox_bak

Postgresql学习笔记(1)

PostgreSQL In BigData BigSQL(整合了pg和hadoop的开源项目) :http://www.bigsql.org/se/ Hadoopdb(耶鲁大学开源项目)SQL to MapReduce to SQL :http://hadoopdb.sourceforge.net/guide/ http://db.cs.yale.edu/hadoo…

酷盘开放API

2019独角兽企业重金招聘Python工程师标准>>> <?php/** * 酷盘api */ class kupan {static $client_id4ca2c199a24ec9a3415f586c77dc****;static $client_secret44edbeca692b0818623476a9b232****;static $backurlhttp://www.baidu.com; static $code"5277…

python常用操作符_Python3.4学习笔记之常用操作符,条件分支和循环用法示例

本文实例讲述了Python3.4常用操作符,条件分支和循环用法。分享给大家供大家参考&#xff0c;具体如下&#xff1a; #Pyhon常用操作符 c d 10 d / 8 #3.x真正的除法 print(d) #1.25 c // 8 #用两个斜杠实现2.x默认的地板除法(整数相除只取整数) print(c) #1 a 3 ** 2 # 3 的 …

php调用平安银行接口,PHP-Java-Bridge的使用(平安银行支付功能专版)

去年做平安银行的时候&#xff0c;用到了PHP-Java-Bridge&#xff0c;后来写了一篇博客记录使用PHP-Java-Bridge的一些心得(连接&#xff1a;http://my.oschina.net/kenblog/blog/316234)。 后来有很多陆陆续续的朋友在做平安银行接口使用PHP-Java-Bridge&#xff0c;看了我那篇…

windows下面安装easy_install和pip教程

asy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安装easy_install 下载地址:https://pypi.python.org/pypi/ez_setup 解压,安装方法cmd进入到对应目录下&#xff0c;执行命令&#xff1a;python ez_setup.py ----------------------------------…

python3第三方库手册_python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑...

python3.4学习笔记(八) Python第三方库安装与使用&#xff0c;包管理工具解惑 许多人在安装Python第三方库的时候, 经常会为一个问题困扰:到底应该下载什么格式的文件? 当我们点开下载页时, 一般会看到以下几种格式的文件: msi, egg, whl msi文件:Windows系统的安装包, 在W…