【发布时间】:2017-03-11 08:57:42
【问题描述】:
我想将一个字符串的一部分合并到 Arraylist 中另一个匹配的字符串
例如
输入
20 E 87th Street/New York//123
20 e 87th street/new york/USA/123
在上面的输入中,除了它缺少一个子字符串 USA,
之外,该行是相同的所以我想把那个美国合并到那条线
输出应该是
20 E 87th Street/New York/USA/123
20 e 87th street/new york/USA/123
我拥有的代码
for (String address : array_adrress)
{
String str1="";
String new_country="";
String new_string="";
int pos_city=address.indexOf("/");
int pos_zip=address.lastIndexOf("/");
String zip=address.substring(pos_zip);
String address=address.substring(pos_city,pos_zip);
int pos_country=address.lastIndexOf("/");
String country=address.substring(pos_country+1);
if(country.isEmpty())
{
str1=check_example(address);
}
if(str1!="")
{
new_country=str1;
}
else new_country=country;
if(address.indexOf("//")>0)
{
new_string=address.substring(0,address.indexOf("//")+2);
}
else
new_string=address.substring(0,n5);
new_string=new_string+new_country+zip;
}
public static String check_example(String s)throws IOException
{
for (String address : array_adrress)
{
if(address.toLowerCase().contains(s.toLowerCase()))
{
String str1="";
String new_country="";
String new_string="";
int pos_city=address.indexOf("/");
int pos_zip=address.lastIndexOf("/");
String zip=address.substring(pos_zip);
String address=address.substring(pos_city,pos_zip);
int pos_country=address.lastIndexOf("/");
String country=address.substring(pos_country+1);
if(!country.isEmpty())
{
return country;
}
}
}
return "";
}
【问题讨论】:
-
那么请分享您到目前为止创建的代码。这个网站不是你放弃你的要求和其他人为你编码的地方。我们希望您展示自己的努力并准确指出您遇到的问题。
-
你能分享你尝试过的代码吗,我们是来修复你代码中的错误而不是在你家编码
标签: java string arraylist merge substring