[英]Split String from the last iteration
這篇文章是對這篇文章的更新: 使用正則表達式獲取字符串中的特定字符並刪除未使用的零
首先,我想用正則表達式刪除最后一場比賽中未使用的零。
我發現正則表達式對於我需要的東西來說有點矯枉過正。
這是我現在想要的,
我想使用split()
方法
從中得到:
String myString = "2020-LI50532-3329-00100"
這個:
String data1 = "2020"
String data2 = "LI50532"
String data3 = "3329"
String data4 = "00100"
那么我可以從 LAST data
中刪除未使用的零
將“00100”轉換為“100”
然后連接所有數據得到這個
"2020-LI50532-3329-100"
我不熟悉拆分方法,如果有人能啟發我這個^^
您可以使用substring
方法擺脫前導零...
String myString = "2020-LI50532-3329-00100";
String[] data = myString.split("-");
data[3] = data[3].substring(2);
StringBuilder sb = new StringBuilder();
sb.append(data[0] + "-" + data[1] + "-" + data[2] + "-" + data[3]);
String result = sb.toString();
System.out.println(result);
假設我們只想刪除最后一個塊的前導零,也許我們可以:
像這樣的東西:
public String removeLeadingZeroesFromLastBlock(String text) {
int indexOfLastDelimiter = text.lastIndexOf('-');
if (indexOfLastDelimiter >= 0) {
String lastBlock = text.substring(indexOfLastDelimiter + 1);
String lastBlockWithoutLeadingZeroes = String.valueOf(Integer.valueOf(lastBlock)); // will throw exception if last block is not an int
return text.substring(0, indexOfLastDelimiter + 1).concat(lastBlockWithoutLeadingZeroes);
}
return text;
}
public class Test {
public static void main(String[] args) {
System.out.println(trimLeadingZeroesFromLastPart("2020-LI50532-03329-00100"));
}
private static String trimLeadingZeroesFromLastPart(String input) {
String delem = "-";
String result = "";
if (input != null && !input.isEmpty()) {
String[] data = input.split(delem);
StringBuilder tempStrBldr = new StringBuilder();
for (int idx = 0; idx < data.length; idx++) {
if (idx == data.length - 1) {
tempStrBldr.append(trimLeadingZeroes(data[idx]));
} else {
tempStrBldr.append(data[idx]);
}
tempStrBldr.append(delem);
}
result = tempStrBldr.substring(0, tempStrBldr.length() - 1);
}
return result;
}
private static String trimLeadingZeroes(String input) {
int idx;
for (idx = 0; idx < input.length() - 1; idx++) {
if (input.charAt(idx) != '0') {
break;
}
}
return input.substring(idx);
}
}
Output:
2020-LI50532-3329-100
您可以將最后一個字符串部分轉換為 integer 類型,如下所示,以刪除未使用的零:
String myString = "2020-LI50532-3329-00100";
String[] data = myString.split("-");
data[3] = data[3].substring(2);
StringBuilder sb = new StringBuilder();
sb.append(data[0] + "-" + data[1] + "-" + data[2] + "-" + Integer.parseInt(data[3]));
String result = sb.toString();
System.out.println(result);
public class Main {
public static void main(String[] args) {
// Test
System.out.println(parse("2020-LI50532-3329-00100"));
System.out.println(parse("2020-LI50532-3329-00001"));
System.out.println(parse("2020-LI50532-03329-00100"));
System.out.println(parse("2020-LI50532-03329-00001"));
}
static String parse(String str) {
return str.replaceAll("0+(?=[1-9]\\d*$)", "");
}
}
Output:
2020-LI50532-3329-100
2020-LI50532-3329-1
2020-LI50532-03329-100
2020-LI50532-03329-1
正則表達式的解釋:
一個或多個零后跟一個非零數字,該數字后跟任意數字,直到字符串結尾(由$
指定)。
您也可以使用Integer.parseInt
來實現,它可以將00100
之類的字符串解析為100
。
public class Main {
public static void main(String[] args) {
// Test
System.out.println(parse("2020-LI50532-3329-00100"));
System.out.println(parse("2020-LI50532-3329-00001"));
System.out.println(parse("2020-LI50532-03329-00100"));
System.out.println(parse("2020-LI50532-03329-00001"));
}
static String parse(String str) {
String[] parts = str.split("-");
try {
parts[parts.length - 1] = String.valueOf(Integer.parseInt(parts[parts.length - 1]));
} catch (NumberFormatException e) {
// Do nothing
}
return String.join("-", parts);
}
}
Output:
2020-LI50532-3329-100
2020-LI50532-3329-1
2020-LI50532-03329-100
2020-LI50532-03329-1
您應該盡可能避免字符串操作,並依賴 Java 語言中的現有類型。 一種這樣的類型是 Integer。 看起來您的代碼由 4 個部分組成 - 年份(整數) - 字符串 - Integer - Integer。
因此,為了正確驗證它,我將使用以下代碼:
Scanner scan = new Scanner("2020-LI50532-3329-00100");
scan.useDelimiter("-");
Integer firstPart = scan.nextInt();
String secondPart = scan.next();
Integer thirdPart = scan.nextInt();
Integer fourthPart = scan.nextInt();
或者類似的東西:
String str = "00100";
int num = Integer.parseInt(str);
System.out.println(num);
如果您想重建原始值,您可能應該使用 NumberFormat 來添加缺失的 0。
要點是:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.