我有一个包含IPCM_20120223_xml.tar.gz的字符串,我想将其更改为USLF_20120223_xml.tar.gz如何只更改IPCM到USLF的前缀?
Here is 2 ways to do it.
def inital = "IPCM_20120223_xml.tar.gz"
def result1 = inital.replaceFirst("IPCM_", "USLF_")
def result2 = "USLF${inital.substring(4)}"
assert "USLF_20120223_xml.tar.gz" == result1
assert "USLF_20120223_xml.tar.gz" == result2
Depending on what generates the initial name for you you may want one over the other.
First way would be good if you know it always starts with "IPCM_" and that character sequence is never anywhere but on the front.
Second way would be good if you know it always starts with a 4 letter sequence and you don't care what it is but you want to change it to USLF.
def inital = "IPCM_20120223_xml.tar.gz"
def result3 = 'USLF'+inital-'IPCM'
assert "USLF_20120223_xml.tar.gz" == result3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.