集册 Java实例教程 通过索引为StringBuilder设置char值

通过索引为StringBuilder设置char值

欢马劈雪     最近更新时间:2020-01-02 10:19:05

792
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
通过索引为StringBuilder设置char值

public class ComputeResult {

    public static void main(String[] args) {

        String original = "software";

        StringBuilder result = new StringBuilder("hi");
        /**
         * N o  w  J a v a . c o m - 时  代  Java 提 供 
        **/

        int index = original.indexOf('a');


        /*1*/result.setCharAt(0, original.charAt(0));

        /*2*/result.setCharAt(1, original.charAt(original.length() - 1));

        /*3*/result.insert(1, original.charAt(4));

        /*4*/result.append(original.substring(1, 4));

        /*5*/result

                .insert(3, (original.substring(index, index + 2) + " "));


        System.out.println(result);

    }

}


展开阅读全文