簡體   English   中英

如何從 groovy 中的字符串替換方括號?

[英]how to replace square brackets from a string in groovy?

我有一個字符串,我使用findAll()從中只得到 6 個字符的數字,但我以這種格式返回數字[111111]我想去掉方括號並將其保存到變量中。 我已經嘗試過了,但它沒有刪除括號:

def msg = "Fix for bug 861768 and 3v3 player number 24"
def bugNumber = msg.findAll( /\d{6}/ ).toString()
bugNumber.replaceAll("\\[", "").replaceAll("\\]","");
println bugNumber

這是我得到的 output: [861768]

不用說我剛開始使用 groovy 所以可能代碼有一些錯誤。 但任何幫助表示贊賞

msg.findAll( /\d{6}/ )返回找到的字符串數組。

如果您只想找到第一個元素,請使用msg.findAll( /\d{6}/ ) [0]

msg.find( /\d{6}/ )

如果您希望所有這些都用逗號分隔msg.findAll( /\d{6}/ ).join(',')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM