Java (六、String类和StringBuffer)

Java String 类

字符串广泛应用 在Java 编程中,在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串。
创建字符串
        // ==比较的是字符串在栈中存放的首地址,而equals()比较的是两个字符串的内容是否相同
    //普通的声明字符串的方法,只会在字符串常量池里开辟空间,并且开辟空间之前,会检查字符串常量池里是否已存在 //相同的数据,如果有,直接指向已存在的数据,如果没有会在字符串常量池里开辟一个新的空间 String s = "ccy"; String s2 ="ccy"; System.out.println(s == s2); System.out.println(s.equals(s2)); //实例化声明字符串的方法,会先在堆中存放数据,将数据的首地址保存在栈内,然后检查字符串常量池是否存在 //相同的数据,如果没有则会在字符串常量池中开辟一个新的空间来存放字符串数据,如果有则声明完毕。 String s3 = new String("jredu"); String s4 = new String("jredu"); System.out.println(s3 == s4); System.out.println(s3.equals(s4)); String s5 = "jereh"; String s6 = new String("jereh"); System.out.println(s5 == s6); System.out.println(s5.equals(s6));

运行图:

关于字符串在内存空间中存放的示例图:

String 方法:

1、字符串长度

用于获取有关对象的信息的方法称为访问器方法。
String 类的一个访问器方法是 length() 方法,它返回字符串对象包含的字符数。
String string = new String("字符串长度");
int length = string.length();
System.out.println(length);

运行图:

2、连接字符串

String 类提供了连接两个字符串的方法:
1、string1.concat(string2);返回 string2 连接 string1 的新字符串。也可以对字符串常量使用 concat() 方法
2、更常用的是使用'+'操作符来连接字符串

下面是一个例子:

public class Demo06 {
    public static void main(String[] args) {
        String s = new String("张三,");
        String hello = new String("你好!");
        String sentence = s.concat(hello);
        System.out.println(sentence);
    }
}

 运行图:

3、分割字符串成数组 

string.split(参数);参数指从哪个字符分割
例子:
package day6;
import java.util.Arrays;
public class Demo09 {
    public static void main(String[] args) {
        String sing = "长亭外 古道边 芳草碧连天 晚风拂柳笛声残 夕阳山外山 ";
        String[] printsing;
        printsing = sing.split(" ");
        System.out.println(Arrays.toString(printsing));
    }
}

运行图:

4、 toUpperCase()让小写的字符串变为大写的字符串

toLowerCase()让大写的字符串变为小写的字符串

equalsIgnoreCase()字符串忽略大小写进行比较

例子:

public class test {
    public static void main(String[] args) {
        String s1 = "abc";
        String s2 = "ABC";
        String s3 = s1.toUpperCase();
        String s4 = s2.toLowerCase();
        System.out.println(s3);
        System.out.println(s4);
        System.out.println(s1.equals(s4));
        System.out.println(s2.equals(s3));
        System.out.println(s1.equalsIgnoreCase(s2));
    }
}

运行图:

5、其他常用的方法 

a、indexOf()获取字符串中某个字符或字符串首次出现的位置,若没有出现则返回-1
b、lastIndexOf()获取字符串中某个字符或字符串最后一次出现的位置,若没有出现则返回-1
c、substring()从字符串的那个索引开始截取,获得一个新的字符串;两个参数的第一个参数是截取开 始的位置(包含),第二个参数是结束的位置(不包含)
d、trim()去掉字符串或者字符前后的空格
例子:
public class Demo08 {
    public static void main(String[] args) {
        String s = "我爱北京天安门!天安门上太阳升!";
        //indexOf()获取字符串中某个字符或字符串首次出现的位置,若没有出现则返回-1
        System.out.println(s.indexOf("天"));
        //lastIndexOf()获取字符串中某个字符或字符串最后一次出现的位置,若没有出现则返回-1
        System.out.println(s.lastIndexOf("天"));
        //substring()从字符串的那个索引开始截取,获得一个新的字符串
        String newS = s.substring(8);
        System.out.println(newS);
        //两个参数的第一个参数是截取开始的位置(包含),第二个参数是结束的位置(不包含)
        String newS3 = s.substring(2, 4);
        System.out.println(newS3);
        //trim()去掉字符串或者字符前后的空格
        String s2 = "   杰 瑞 教 育        ";
        String newS4 = s2.trim();
        System.out.println(s2);
        System.out.println(newS4);
    }
}

运行图:

StringBuffer 类

当对字符串进行修改的时候,需要使用 StringBuffer
和 String 类不同的是,StringBuffer类的对象能够被多次的修改,并且不产生新的未使用对象。

StringBuffer的创建

例子:
StringBuffer sBuffer = new StringBuffer("青春无悔");
System.out.println(sBuffer);

运行图:

StringBuffer和String的区别:

Java 中 StringBuffer 和 String 是有一定的区别的,首先,String 是被 final 修饰的,他的长度是不可变的,就算调用 String 的
concat 方法,那也是把字符串拼接起来并重新创建一个对象,把拼接后的 String 的值赋给新创建的对象,而 StringBuffer 的长度是可变的,调用StringBuffer 的 append 方法,来改变 StringBuffer 的长度,并且,相比较于 StringBuffer,String 一旦发生长度变化,是非常耗费内存的!
StringBuffer和String之间的转换:
例子:
public static void main(String[] args) {
        String s = "conversion"
        //String ---> StringBuffer
        StringBuffer sb = new StringBuffer(s);
        //StringBuffer ---> String
        String s2 = sb.toString();
        System.out.println(sb);
  System.out.println(s2);
        scanner.close();
    }

运行图:

StringBuffer的常用方法:

a、append(string s)将指定的字符串追加到此字符序列。

public static void main(String[] args) {
        StringBuffer buffer = new StringBuffer("abcde");
        buffer.append("f");
        System.out.println(buffer);
}

运行图:

b、 reverse() 将此字符序列用其反转形式取代。

public static void main(String[] args) {
        StringBuffer sBuffer = new StringBuffer("abcde");
        sBuffer.reverse();
        System.out.println(sBuffer);
    }

运行图:

c、 delete(int start, int end) 移除此序列的子字符串中的字符。

public class Demo04 {
    public static void main(String[] args) {
        StringBuffer sBuffer = new StringBuffer("abcde");
        sBuffer.delete(1, 3);
        System.out.println(sBuffer);
    }
运行图:

d、 insert(int offset, int i) 将 int 参数的字符串表示形式插入此序列中。

public static void main(String[] args) {
        StringBuffer sBuffer = new StringBuffer("abcde");
        sBuffer.insert(2,"!");
        System.out.println(sBuffer);
    }

运行图:

e、replace(int start, int end, String str)使用给定 String 中的字符替换此序列的子字符串中的字符。

public static void main(String[] args) {
        StringBuffer sBuffer = new StringBuffer("abcde");
        sBuffer.replace(1, 4,"*");
        System.out.println(sBuffer);
    }

运行图:

 

 

 

posted on 2017-08-23 22:25  ★【金字塔】☆  阅读(287)  评论(0编辑  收藏  举报