如何在servlet中拆分这个字符串?

时间:2013-11-11 13:54:55

标签: java servlets

我有以下3 countryList

  Korea, Republic of
  Iran, Islamic Republic of,
  Virgin Islands, U.S.

我通过$.ajax()方法发送

countryList: "Korea, Republic of,Iran, Islamic Republic of,Virgin Islands, U.S."

在我的servlet中我有代码..

       if(request.getParameterMap().containsKey("countryList")){
                 String countryList = request.getParameter("countryList");
                 String [] countries = splitCountries(countryList);
                 for(int i = 0;i < countries.length;i++){
                     String currentCountry = countries[i];
                     dao.addCountry(username, currentCountry);
                 }
            }


     public String[] splitCountries(final String Countries)
     {
       return Countries.split(",");
     }

我想要整个3国名作为结果,但它没有给出?

1 个答案:

答案 0 :(得分:0)

首先,你在字符串中自己输了一个逗号;)

Korea, Republic of
Iran, Islamic Republic of ,  <-- the comma at the end  
Virgin Islands, U.S.

其次,如果这是你在Servlet中获取的字符串“countryList”:

  

countryList:“韩国,伊朗共和国,伊朗伊斯兰共和国,维尔京   群岛,美国“

这意味着字符串早先已经搞砸了,可能是在AJAX调用中。这个字符串已经添加了一些不需要的逗号。

你能展示你的AJAX方法吗?特别是它将3'行'串联到1个字符串的部分。