欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java中replaceAll替換圓括號(hào)實(shí)例代碼

 更新時(shí)間:2022年10月09日 11:40:33   作者:天狼1222  
正則表達(dá)式的保留字符主要有:圓括號(hào)、方括號(hào)、花括號(hào)、豎線、橫線、點(diǎn)號(hào)、加號(hào)、星號(hào)、反斜桿等等,下面這篇文章主要給大家介紹了關(guān)于java中replaceAll替換圓括號(hào)的相關(guān)資料,需要的朋友可以參考下

前言

在手寫sql的時(shí)候,根據(jù)參數(shù)處理查詢條件.

select * from staff where 1 = 1 and staff_id in ($staffIds) 
 and staff_name in ($staffNames)

比如staffId為空,需要把staff_id in ($staffIds) 候設(shè)置為true,staffName正常賦值

replace替換圓括號(hào)

public static void main(String[] args) {
    String sqlStr = "select * from staff where 1 = 1 and staff_id in ($staffIds) " +
            "and staff_name in ($staffNames)";
    String replaceEmpty = "staff_id in ($staffIds)";
    String replaceSql = sqlStr.replace(replaceEmpty, "true");
    System.out.println(replaceSql);
}

結(jié)果:

select * from staff where 1 = 1 and true and staff_name in ($staffNames)

直接用replace就可以了。

replaceAll替換圓括號(hào)

如果是想用replaceAll呢?

public static void main(String[] args) {
    String sqlStr = "select * from staff where 1 = 1 and staff_id in ($staffIds) " +
            "and staff_name in ($staffNames)";
    String replaceEmpty = "staff_id in ($staffIds)";
    String replaceAllSql =  sqlStr.replaceAll(Matcher.quoteReplacement(replaceEmpty), "true");
    System.out.println(replaceAllSql);
}

結(jié)果:

select * from staff where 1 = 1 and staff_id in ($staffIds) 
and staff_name in ($staffNames)

沒有替換成功,為什么呢?

看replaceAll的方法:

public String replaceAll(String regex, String replacement) {
    return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}

regex 對(duì)應(yīng)的是正則的內(nèi)容,因此要對(duì)圓括號(hào)進(jìn)行轉(zhuǎn)移下:

String replaceAllSql =  replaceEmpty =  replaceEmpty.replaceAll("\\(", "[( )]").replaceAll("\\)", "[( )]");

 替換前,對(duì)圓括號(hào)進(jìn)行轉(zhuǎn)義

public static void main(String[] args) {
    String sqlStr = "select * from staff where 1 = 1 and staff_id in ($staffIds) " +
            "and staff_name in ($staffNames)";
    String replaceEmpty = "staff_id in ($staffIds)";
    replaceEmpty =  replaceEmpty.replaceAll("\\(", "[( )]").replaceAll("\\)", "[( )]");
    String replaceAllSql =  sqlStr.replaceAll(Matcher.quoteReplacement(replaceEmpty), "true");
    System.out.println(replaceAllSql);
}

結(jié)果:

select * from staff where 1 = 1 and true and staff_name in ($staffNames)

替換成功。

補(bǔ)充:Java 利用replaceAll 替換中括號(hào)

Java的replaceAll函數(shù)默認(rèn)是不能替換中括號(hào)的,例如想替換[b]到<b>,結(jié)果卻就變成[<b>]

解決方案就是首先利用正則表達(dá)式替換中括號(hào),然后再替換中括號(hào)內(nèi)的內(nèi)容:

infos = infos.replaceAll("[\\[\\]]","");  

不過后來又查詢了下資料,發(fā)現(xiàn)中括號(hào)在java中居然是特殊字符,一對(duì)中括號(hào)里的內(nèi)容是一組正則表達(dá)式。所以如果打算讓[b]-><b>,只要如下寫法:

infos = infos.replaceAll("\\[b\\]","<b>");  

總結(jié):

字符替換的時(shí)候,優(yōu)先考慮使用replace,多個(gè)時(shí)候,也是生效的。如果要使用replace的話,使用要注意特殊字符的處理?;蛘咦约簩懻齽t進(jìn)行處理。

優(yōu)化: 《整體替換sql》

到此這篇關(guān)于java中replaceAll替換圓括號(hào)的文章就介紹到這了,更多相關(guān)java replaceAll替換圓括號(hào)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解IDEA中MAVEN項(xiàng)目打JAR包的簡單方法

    詳解IDEA中MAVEN項(xiàng)目打JAR包的簡單方法

    本篇文章主要介紹了詳解IDEA中MAVEN項(xiàng)目打JAR包的簡單方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • SpringBoot中EasyExcel實(shí)現(xiàn)Excel文件的導(dǎo)入導(dǎo)出

    SpringBoot中EasyExcel實(shí)現(xiàn)Excel文件的導(dǎo)入導(dǎo)出

    這篇文章主要介紹了SpringBoot中EasyExcel實(shí)現(xiàn)Excel文件的導(dǎo)入導(dǎo)出,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • SpringBoot動(dòng)態(tài)修改yml配置文件的方法詳解

    SpringBoot動(dòng)態(tài)修改yml配置文件的方法詳解

    這篇文章主要為大家詳細(xì)介紹了SpringBoot動(dòng)態(tài)修改yml配置文件的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Java并發(fā)編程數(shù)據(jù)庫與緩存數(shù)據(jù)一致性方案解析

    Java并發(fā)編程數(shù)據(jù)庫與緩存數(shù)據(jù)一致性方案解析

    這篇文章主要為大家介紹了Java并發(fā)編程中數(shù)據(jù)庫與緩存數(shù)據(jù)一致性解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-04-04
  • Java?-jar參數(shù)詳解之掌握J(rèn)ava可執(zhí)行JAR文件的運(yùn)行技巧

    Java?-jar參數(shù)詳解之掌握J(rèn)ava可執(zhí)行JAR文件的運(yùn)行技巧

    做項(xiàng)目的時(shí)候我們肯定接觸過很多jar包,下面這篇文章主要給大家介紹了關(guān)于Java?-jar參數(shù)詳解之掌握J(rèn)ava可執(zhí)行JAR文件的運(yùn)行技巧,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • 淺談Java并發(fā)之同步器設(shè)計(jì)

    淺談Java并發(fā)之同步器設(shè)計(jì)

    這篇文章主要介紹Java并發(fā)之同步器設(shè)計(jì),本文以記錄方式并發(fā)編程中同步器設(shè)計(jì)的一些共性特征。并簡單介紹了Java中的AQS,需要的朋友可以參考一下文章的詳細(xì)內(nèi)容
    2021-10-10
  • java基于quasar實(shí)現(xiàn)協(xié)程池的方法示例

    java基于quasar實(shí)現(xiàn)協(xié)程池的方法示例

    本文主要介紹了java基于quasar實(shí)現(xiàn)協(xié)程池的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>
    2022-06-06
  • Spring MVC 攔截器實(shí)現(xiàn)代碼

    Spring MVC 攔截器實(shí)現(xiàn)代碼

    本篇文章主要介紹了Spring MVC 攔截器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-02-02
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(5)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(5)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-07-07
  • 如何為Spark Application指定不同的JDK版本詳解

    如何為Spark Application指定不同的JDK版本詳解

    這篇文章主要給大家介紹了關(guān)于如何為Spark Application指定不同的JDK版本的相關(guān)資料,文中通過示例代碼將解決的方法介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面來隨著小編一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11

最新評(píng)論