替换字符串某个元素Java

替换字符串-Java

    @Test
    public void test03() {
    
    
        // 替换字符串
        String str = "hello hello world";

        // 替换所有的,且不改变原始字符串
        System.out.println(str.replace("he", "ha"));
        // 只替换第一此出现的
        System.out.println(str.replaceFirst("llo", "lle"));
        // 替换所有的
        System.out.println(str.replaceAll("ll", "mm"));
    }

hallo hallo world
helle hello world
hemmo hemmo world

Guess you like

Origin blog.csdn.net/E_chos/article/details/114641140