StringBuilder method to remove the last of an extra character

public class StringBuilterTest {
    public static void main(String[] args) {

        //新增一个map
        Map<String, String> map = new HashMap<>();
        map.put("code", "500");
        map.put("name", "测试");
        map.put("age", "100");


        //遍历map拼接字符串
        StringBuilder stringBuilder = new StringBuilder();
        for (String key : map.keySet()){
            stringBuilder.append(key).append("=").append(map.get(key)).append("&");
        }


        // then we tend to want to remove the last ampersand (&), as follows: When testing To test these two methods separately; I used method 1 
        System.out.println (stringBuilder.deleteCharAt (stringBuilder.length () - 1) .toString ()); // process (delete then turn string, this use), the result: code = 500 & name = test Age = 100 & 
        System.out.println (stringBuilder.substring (0, stringBuilder.length () - 1 )); // method 2 method (replication efficiency may be low), results: code = 500 & name = test Age = 100 & 


    } 
}

 

 

Guess you like

Origin www.cnblogs.com/del88/p/12040223.html