详谈String类

来源:互联网 发布:js 所有input不可编辑 编辑:程序博客网 时间:2024/06/14 00:35

String类概述:
String 类代表字符串。Java 程序中的所有字符串字面值(如 “abc” )都作为此类的实例实现。字符串是由多个字符组成的一串数据(字符序列),字符串可以看成是字符数组。

String类的构造方法:
public String ():空构造
public String(byte[] bytes):把字节数组转成字符串
public String(byte[] bytes, int index,int length):把字节数组的一部分转成字符串
public String(char[] value):把字符数组转成字符串
public String(byte[] bytes, int index,int length):把字符数组的一部分转成字符串

举例:

public class StringDemo {    public static void main(String[] args) {        //public String ():空构造        String s1=new String();        System.out.println("s1:"+s1);        System.out.println("s1.length():"+s1.length());        System.out.println("-------------");        //public String(byte[] bytes):把字节数组转成字符串        byte[] bytes={97,98,99,100,103,109,105,104};        String s2=new String(bytes);        System.out.println("s2:"+s2);        System.out.println("s2.length():"+s2.length());        System.out.println("-------------");        // public String(byte[] bytes, int index,int length)        String s3=new String(bytes,1,3);        System.out.println("s3:"+s3);        System.out.println("s3.length():"+s3.length());        System.out.println("-------------");        //public String(char[] value):把字符数组转成字符串        char[] chars={'h','e','l','l','o','胡','歌','!'};        String s4=new String(chars);        System.out.println("s4:"+s4);        System.out.println("s4.length():"+s4.length());        System.out.println("-------------");        //public String(byte[] bytes, int index,int length)把字符数组的一部分转成字符串        String s5=new String(chars,1,4);        System.out.println("s5:"+s5);        System.out.println("s5.length():"+s5.length());        System.out.println("-------------");    }}

输出:

s1:s1.length():0-------------s2:abcdgmihs2.length():8-------------s3:bcds3.length():3-------------s4:hello胡歌!s4.length():8-------------s5:ellos5.length():4-------------

String类重写了toString()方法:

public String toString() {        return this;    }

String字面值对象和构造方法创建对象的区别:

        String str1="hello";        String str2=new String("hello");        System.out.println(str1==str2);//false        System.out.println(str1.equals(str2));//true

字面值对象直接在方法区的字符串常量池中找,有就用,没有就创建;
构造方法创建对象会创建两个对象,堆内存中一个,方法区的字符串常量池中一个(有就用,没有再创建)。
这里写图片描述

String类的判断功能:
boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
boolean contains(String str):判断大字符串中是否包含小字符串
boolean startWith(String str):判断字符串是否以某个指定字符串开头
boolean endWith(String str):判断字符串是否以某个指定字符串结尾
boolean isEmpty():判断字符串是否为空

关于isEmpty()及”“与null的区别:

public class StringDemo {    public static void main(String[] args) {        String s1="";//创建对象,内容为空        String s2=null;//空指针,对象不存在        System.out.println(s1.isEmpty());//true        System.out.println(s2.isEmpty());//NullPointerException空指针异常,对象s2不存在,不能调方法    }}

String类的获取功能:
int length():获取字符串长度
char charAt(int index):获取指定索引位置的字符
int indexOf(int ch):返回指定字符在此字符串中第一次出现初的索引,()中可以是int类型也可以是char类型
int indexOf(String str):返回指定字符串在此字符中第一次出现处的索引
int indexOf(int ch ,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引
int indexOf(String str ,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引
String substring(int start):从指定位置开始截取字符串,默认到结尾
String substring(int start,int end):从指定位置开始到指定位置结束截取字符串(包含start索引不包含end索引)

String类的转换功能:
byte[] getBytes():把字符串转换成字节数组
char[] toCharArray():把字符串转换成字符数组
static String valueOf(char[] chs):把字符数组转换成字符串
static String valueOf(int i):把int类型的数据转换成字符串
String类的valueOf方法可以把任意类型的数据转换成字符串
String toLowerCase():把字符串转换成小写
String toUpperCase():把字符串转换成大写
String concat(String str):把字符串拼接

String类的其他功能:
替换功能:
String replace (char old,char new)
String replace(String old,String new)

去除字符串两端空格
Sring trim()

按字典顺序比较两个字符串
int compareTo(String str)
int compareToIgnoreCase(String str)

0 0
原创粉丝点击