如何从字符串中替换像“¶”这样的特殊字符并给它们正确的字符“Ö”?

时间:2014-02-27 07:04:46

标签: java android google-geocoder

嘿我使用反向谷歌地理编码器,当城市名称为例如“Königsaich”时,地理编码器将其命名为“Königsaich”。如何过滤“¶”字符并将其替换为正确的字符“ö”?

修改

我的代码:

public JSONObject getJSONCity(double lat, double lon){

         HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lon+"&sensor=false");
            HttpClient client = new DefaultHttpClient();
            HttpResponse response;
            StringBuilder stringBuilder = new StringBuilder();

            try {
                response = client.execute(httpGet);
                HttpEntity entity = response.getEntity();
                InputStream stream = entity.getContent();
                int b;
                while ((b = stream.read()) != -1) {
                    stringBuilder.append((char) b);
                }
            } catch (ClientProtocolException e) {
                } catch (IOException e) {
            }

            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject = new JSONObject(stringBuilder.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return jsonObject;
    }


    public String getCityname(double lat, double lon){

        JSONObject ret = getJSONCity(lat,lon); 
         JSONArray jsonArray;

         JSONObject location;
         String location_string;

         try {
             jsonArray = ret.getJSONArray("results").getJSONObject(0).getJSONArray("address_components");
             cityname = jsonArray.getJSONObject(2).getString("long_name");

             //Get JSON Array called "results" and then get the 0th complete object as JSON        
             location = ret.getJSONArray("results").getJSONObject(0); 
             // Get the value of the attribute whose name is "formatted_string"
             location_string = location.getString("formatted_address");

             System.out.println(" results: " + location_string);

             System.out.println(" My City: " + cityname);
         } catch (JSONException e1) {
             e1.printStackTrace();
         }
         return cityname;
    }

有谁能举个例子?

4 个答案:

答案 0 :(得分:0)

String actualstring ="what ever your string which you want to replace here get ";
         if(actualstring.contains("here your string which u want to chk"));
                { 
                   // it chk weather it contain that string which u want to replace 
           actualstring.replace("target", "replacement");
                 }

答案 1 :(得分:0)

您可以使用: String rightName = responseName.replace(“¶”,“ö”);

更多细节: http://www.tutorialspoint.com/java/java_string_replace.htm

答案 2 :(得分:0)

试试这个

str = str.replace(' ','_');

public String replace(CharSequence target,CharSequence replacement) 在API级别1中添加 复制此字符串,将指定目标序列的出现次数替换为另一个序列。字符串从头到尾进行处理。

参数 定位要替换的序列。 更换更换顺序。

返回 结果字符串。 抛出 如果target或replacement为null,则为NullPointerException。

答案 3 :(得分:0)

您的问题是您正在从HttpEntity读取字节,并假设每个字节都是单个字符。根据您的编码,这可能适用于ASCII字符,但很可能会因许多其他字符而失败。

要正确执行此操作,您需要使用Apache的HTTP Utils库等库,而不是设置复杂的搜索/替换组合。它包含一个类EntityUtils,它有一些从HttpEntity读取字符串的方法。

它的Javadoc位于http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/util/EntityUtils.html