字符串深析

截取字符串:substring(args1,args2);

substring(args1,args2);

如:截取“HelloWorld”字符串的Hello;

        String string = "HelloWorld";
        System.out.print(string.substring(0,5));

比较两个字符串是否相等:equals();

equals比较的是值,==比较的是内存

sting1.equals(string2);

如:比较string1和string2

        String string1 = "Hello";
        String string2 = "Hello";
        //new 的变量都储存在堆内存里
        String string3 = new String("Hello");
        System.out.println(string1 == string2);
        System.out.print(string1.equals(string2));
        System.out.print(string1.equals(string2));
        System.out.println(string1 == string3);
        

你可能感兴趣的:(字符串深析)