在Groovy中進行數字替換

2021-08-29 13:04:21 字數 452 閱讀 9846

在處理數字的時候,經常需要將單個的數字前加個「0」以補充成標準的兩位形式,比如將「3「轉換成為」03「,這在日期中很常見,當然在日期中也有響應的格式可以解決,在此只是用groovy的replaceall來在給定的字串中去替換。

**很簡單,也沒有啥好說的,只是注意,在此用到了正規表示式(groovy的[url=請看這裡[/url]),**如下:

def s = "6.1.2.10"

assert "06.01.02.10"==s.replaceall("[\\d]|[\\d]",)

或者更簡潔的方法(by[url= macrochen [/url]):

assert "06.01.02.10「 == s.replaceall ("[\\d]+", )

在VIM中進行文字替換

在vim中進行文字替換 1.替換當前行中的內容 s from to s即substitude s from to 將當前行中的第乙個from,替換成to。如果當前行含有多個 from,則只會替換其中的第乙個。s from to g 將當前行中的所有from都替換成to。s from to gc 將當...

在VIM中進行文字替換

在vim中進行文字替換 1.替換當前行中的內容 s from to s即substitude s from to 將當前行中的第乙個from,替換成to。如果當前行含有多個 from,則只會替換其中的第乙個。s from to g 將當前行中的所有from都替換成to。s from to gc 將當...

在VIM中進行文字替換

原文 1.替換當前行中的內容 s from to s即substitude s from to 將當前行中的第乙個from,替換成to。如果當前行含有多個 from,則只會替換其中的第乙個。s from to g 將當前行中的所有from都替換成to。s from to gc 將當前行中的所有fro...