gpt4 book ai didi

java - 在状态 abb 之间添加逗号。和地址列表中的邮政编码

转载 作者:行者123 更新时间:2023-12-02 07:24:24 26 4
gpt4 key购买 nike

我认为正则表达式为“[A-Z]{2} [0-9]{5}”。我如何浏览列表并在两者之间添加逗号?州缩写和邮政编码之间。

我应该找到正则表达式的位置,然后在索引 2 处添加一个逗号吗?

最佳答案

假设字符串列表在每个字符串的末尾始终有一个缩写和一个邮政编码,您可以循环遍历该列表并调用它,使用返回值来替换现有字符串。

public String addComma(String address) {
String[] tmp = address.split(" ");
String newAddress = "";
int len = tmp.length;
for (int i = 0; i < len - 2; i++) {
//add all the information before the state abbreviation and post code
newAddress = newAddress + tmp[i] + " ";
}
return (newAddress + tmp[len - 2] + ", " + tmp[len - 1]);
}

关于java - 在状态 abb 之间添加逗号。和地址列表中的邮政编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13747007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com