我如何使用StringBuilder

时间:2017-02-08 09:11:37

标签: java android stringbuilder

我从地理编码器获得了用户城市,州和国家/地区。采用以下格式

D/ProfileFrg: StateName: Pune, Maharashtra 411053
D/ProfileFrg: CountryName: India

我想使用StringBuilder将此String格式化为单行: Pune,Maharashtra,India ,其中将删除邮政编码。

在我刚接触之前我没有使用过StringBuilder,任何人都可以帮忙解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

   A better snippet would be as below.

String abc = "D/ProfileFrg: StateName: Pune, Maharashtra 411053" + "D/ProfileFrg: CountryName: India";
        /* split results with below output.
         * D/ProfileFrg StateName Pune, Maharashtra 411053D/ProfileFrg
         * CountryName India
         */
        String[] x = abc.split(":");

        String y = x[2] + ", " + x[4];
        /* y is Pune, Maharashtra 411053D/ProfileFrg, India */

        String[] z = y.split(" ");
        /* z has Pune, Maharashtra 411053D/ProfileFrg,
         * India
         */
        String finalValue = z[1] + " " + z[2] + ", " + z[5];
        System.out.println(finalValue);

答案 1 :(得分:0)

我找到了解决方案来替换字符串中的数字。

String place = stateName + countryName;
        place  = place.replaceAll("[0-9]","");
        Log.d(TAG, place);