我有一个国家
String countryNames ="CHINA,RUSSIAN FEDERATION, THE,INDIA,MALAYSIA";
这里是俄罗斯联邦,THE属于同一个国家/地区名称,我想逐国拆分,我使用的是StringTokenizer,
StringTokenizer st = new StringTokenizer(countryNames,",");
while(st.hasMoreTokens()) {
String countryName = st.nextToken();
countriesList.add(countryName);
}
但是当我必须拆分俄罗斯联邦时,上面的代码失败了,可以请任何人告诉我如何才能实现这一目标?
答案 0 :(得分:2)
您可以使用正则表达式作为分隔符
将分隔符设置为" / \ S,/"它只会找到前面有非空白字符的逗号。
编辑:您也可以将此替换为替换,这意味着您可以将要用作分隔符的所有逗号替换为其他更合适的分隔符(如分号)
StringTokenizer st = new StringTokenizer(countryNames.replace("/\\S,/", ";"),";");
有双反斜杠,所以处理的字符串将包含/ \ S,/
答案 1 :(得分:1)
以下代码可帮助您解决问题,代码未经过优化,请尝试将其缩小并进行优化。
public static void main(String[] args)
{
String countryNames = "CHINA,RUSSIAN FEDERATION, THE,INDIA,MALAYSIA";
List<String> countriesList = new ArrayList<>();
int startIndex = 0, endIndex = 0;
char[] chararray = countryNames.toCharArray();
String str = "";
for (char ch : chararray)
{
if (endIndex > 0)
{
//Find only those commas whose nearby with characters only. Example : E,I
if (ch == ',' && (chararray[endIndex - 1] >= 'A' && chararray[endIndex - 1] <= 'Z')
&& (chararray[endIndex + 1] >= 'A' && chararray[endIndex + 1] <= 'Z'))
{
str = "";
for (int i = startIndex; i < endIndex; i++)
{
str += chararray[i];
}
countriesList.add(str);
startIndex = endIndex + 1;
}
}
endIndex++;
}
str = "";
//Add last segment of the string.
for (int i = startIndex; i < endIndex; i++)
{
str += chararray[i];
}
countriesList.add(str);
//Displaying list.
for (String s : countriesList)
System.out.println(s);
}
答案 2 :(得分:0)
由于,
不明确,代码将始终失败。正确解析此的唯一方法可能是,因为THE,
之前有空格,以区分它是之前标记的一部分。
所以
StringTokenizer st = new StringTokenizer(countryNames,",");
while(st.hasMoreTokens()) {
String countryName = st.nextToken();
if(countryName.startsWith(" ")) // or any other way to distinguish it, possibly with a regex itself
{
String prevToken = countriesList.get(countriesList.size() -1);
String completedCountry = merge(prevToken, countryName); // write some code to merge it to the proper countryname.
replace(countriesList, prevToken, completedCountry); // replace 'prevToken' value with 'completedCountry' in 'countriesList'
} else {
countriesList.add(countryName);
}
}
答案 3 :(得分:0)
您可以使用split
方法解决此问题。
String countryNames ="CHINA,RUSSIAN FEDERATION, THE,INDIA,MALAYSIA";
String str[]=countryNames.split(",");
for(int i=0;i<str.length;i++){
System.out.println(str[i]);
}