public class Test { public static void main(String[] args) { //声明一个字符串 String s = "itcast"; String s1=" i c a s t "; //替换操作 System.out.println("将it替换成cn.it后结果为:"+s.replace("it", "cn.it")); //去空格 System.out.println("去掉左右两边的空格后:"+s1.trim()); //去掉所有空格 System.out.println("去掉所有空格:"+s1.replace(" ", ""));