最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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的話,使用要注意特殊字符的處理。或者自己寫正則進(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)文章

  • 關(guān)于jackson序列化和feign返回值的問題

    關(guān)于jackson序列化和feign返回值的問題

    這篇文章主要介紹了關(guān)于jackson序列化和feign返回值的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • springboot注解Aspect實(shí)現(xiàn)方案

    springboot注解Aspect實(shí)現(xiàn)方案

    本文提供一種自定義注解,來實(shí)現(xiàn)業(yè)務(wù)審批操作的DEMO,不包含審批流程的配置功能。對(duì)springboot注解Aspect實(shí)現(xiàn)方案感興趣的朋友一起看看吧
    2022-01-01
  • Java中ArrayList的removeAll方法詳解

    Java中ArrayList的removeAll方法詳解

    這篇文章主要給大家介紹了關(guān)于Java中ArrayList的removeAll方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來看看吧。
    2017-07-07
  • JAVA maven項(xiàng)目使用釘釘SDK獲取token、用戶

    JAVA maven項(xiàng)目使用釘釘SDK獲取token、用戶

    這篇文章主要介紹了JAVA maven項(xiàng)目使用釘釘SDK獲取token、用戶,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • spring后置通知@AfterReturning的使用

    spring后置通知@AfterReturning的使用

    這篇文章主要介紹了spring后置通知@AfterReturning的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • SpringBoot常見問題小結(jié)

    SpringBoot常見問題小結(jié)

    這篇文章主要介紹了SpringBoot常見問題小結(jié),需要的朋友可以參考下
    2017-07-07
  • Java SSM框架(Spring+SpringMVC+MyBatis)搭建過程

    Java SSM框架(Spring+SpringMVC+MyBatis)搭建過程

    最近一段時(shí)間搭建了ssm環(huán)境,并測(cè)試了幾個(gè)小項(xiàng)目,下面小編通過圖文并茂的形式給大家分享Java SSM框架(Spring+SpringMVC+MyBatis)搭建過程,需要的朋友參考下吧
    2017-11-11
  • Java中的Semaphore信號(hào)量簡(jiǎn)單使用代碼實(shí)例

    Java中的Semaphore信號(hào)量簡(jiǎn)單使用代碼實(shí)例

    這篇文章主要介紹了Java中的Semaphore信號(hào)量簡(jiǎn)單使用代碼實(shí)例,Semaphore是用來保護(hù)一個(gè)或者多個(gè)共享資源的訪問,Semaphore內(nèi)部維護(hù)了一個(gè)計(jì)數(shù)器,其值為可以訪問的共享資源的個(gè)數(shù),一個(gè)線程要訪問共享資源,需要的朋友可以參考下
    2023-12-12
  • 基于Ok+Rxjava+retrofit實(shí)現(xiàn)斷點(diǎn)續(xù)傳下載

    基于Ok+Rxjava+retrofit實(shí)現(xiàn)斷點(diǎn)續(xù)傳下載

    這篇文章主要為大家詳細(xì)介紹了基于Ok+Rxjava+retrofit實(shí)現(xiàn)斷點(diǎn)續(xù)傳下載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Java Spring使用hutool的HttpRequest發(fā)送請(qǐng)求的幾種方式

    Java Spring使用hutool的HttpRequest發(fā)送請(qǐng)求的幾種方式

    文章介紹了Hutool庫中用于發(fā)送HTTP請(qǐng)求的工具,包括添加依賴、發(fā)送GET和POST請(qǐng)求的方法,以及GET請(qǐng)求的不同參數(shù)傳遞方式,感興趣的朋友跟隨小編一起看看吧
    2024-11-11

最新評(píng)論

泗洪县| 新乡县| 佛教| 德昌县| 宣武区| 肇州县| 鄂州市| 古田县| 梁河县| 庆安县| 奇台县| 潞城市| 班玛县| 中方县| 香格里拉县| 余姚市| 明水县| 祥云县| 海口市| 宜章县| 宁陕县| 永靖县| 河池市| 收藏| 安丘市| 黎川县| 游戏| 宁都县| 宁明县| 博客| 绥德县| 滁州市| 蒙自县| 昭苏县| 高碑店市| 冕宁县| 海原县| 体育| 环江| 抚顺县| 伊春市|