String 类

String 类

代表字符串

 

字符串的创建

String str = "hello";

String s2 = new String(String original);

String s3 = new String(char[] a);

String s4 = new String(char[] a,int startIndex,int count);

 

String类的常用方法

public int length():返回当前字符串的长度

public char charAt(int index):获取指定索引位置的字符

public boolean equals(Object anObject):比较两个字符串内容是否相等。

public int compareTo(String anotherString):比较两个字符串的大小

public int indexOf(String s):返回s在当前字符串中首次出现的位置。如果不存在,返回-1.

public int indexOf(String s ,int startpoint):

public int lastIndexOf(String s):返回s在当前字符串中末次出现的位置。如果不存在,返回-1.

public int lastIndexOf(String s ,int startpoint):

public boolean startsWith(String prefix):判断当前的字符串是否以指定的prefix字符串开始的

public boolean endsWith(String suffix):判断当前的字符串是否以指定的suffix字符串结束的

public boolean regionMatches(int firstStart,String other,int otherStart ,int length)

调用regionMatches的字符串从firstStart开始和other字符串从otherStart开始length长的字符是否相等。

 

注:以下方法调用方法的字符串,在方法执行完以后调用字符串不变

public String substring(int startpoint):返回当前字符串中startPoint位置开始,到末尾的子字符串。

public String substring(int start,int end):返回当前字符串中从startPoint位置开始,到end结束的左闭右开区间的子字符串。

pubic String replace(char oldChar,char newChar):将字符串中指定的所有oldChar替换为newChar。

public String replaceAll(String old,String new):将字符串中指定的所有old替换为new。

public String trim():去除字符串首尾的空格。

public String concat(String str):连接两个字符串。

public boolean contains(CharSequence s):判断当前字符串中是否包含s。

public String[] split(String regex):根据给定正则表达式的匹配拆分此字符串。

 

String与其他结构的转换

(1)String 包装类、基本数据类型变量间的转换

字符串 —> 基本数据类型、包装类

Integer包装类的public static int parseInt(String s):可以将由“数字”字符组成的字符串转换为整型。

类似地,使用java.lang包中的Byte、Short、Long、Float、Double类调相应的类方法可以将由“数字”字符组成的字符串,转化为相应的基本数据类型。

 

基本数据类型、包装类 —> 字符串

调用String类的public String valueOf(int n)可将int型转换为字符串

相应的valueOf(byte b)、valueOf(long l)、valueOf(float f)、valueOf(double d)、

valueOf(boolean b)可由参数的相应类型到字符串的转换

 

(2)String 字节数组间的转换

字节数组 —> 字符串

String(byte[]):通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。

String(byte[], int offset, int length):用指定的字节数组的一部分,即从数组起始位置offset开始取length个字节构造一个字符串对象。

 

字符串 —> 字节数组

public byte[] getBytes():使用平台的默认字符集将此 String 编码为byte 序列,并将结果存储到一个新的 byte 数组中。

 

public byte[] getBytes(String charsetName):使用指定的字符集将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。

 

(3)String 字符数组间的转换

字符数组 —> 字符串

String 类的构造器:String(char[]) 和 和 String(char[] ,int offset ,int length) 分别用字符数组中的全部字符和部分字符创建字符串对象。

 

字符串 —> 字符数组

public char[] toCharArray():将字符串中的全部字符存放在一个字符数组中的方法。

public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin):提供了将指定索引范围内的字符串存放到数组中的方法。

 

 

StringBuffer

java.lang.StringBuffer代表可变的字符序列

 

创建

StringBuffer 类不同于String,其对象必须使用构造器生成。有三个构造器:

StringBuffer():初始容量为16 的字符串缓冲区

StringBuffer(int size):构造指定容量的字符串缓冲区

StringBuffer(String str):将内容初始化为指定字符串内容

 

 

常用方法

StringBuffer append(xxx):提供了很多的append()方法,用于进行字符串拼接

StringBuffer delete(int start,int end):删除指定位置的内容

StringBuffer replace(int start, int end, String str):把[start,end)位置替换为str

StringBuffer insert(int offset, xxx):在指定位置插入xxx

StringBuffer reverse():把当前字符序列逆转

 

public int indexOf(String str)

public String substring(int start,int end)

public int length()

public char charAt(int n )

public void setCharAt(int n ,char ch)

 

 

StringBuilder

StringBuilder 和 StringBuffer 非常类似,均代表可变的字符序列,而且提供相关功能的方法 也一样。

 

String、StringBuffer、StringBuilder三者的异同?

String:不可变的字符序列;底层使用char[]存储。

StringBuffer:可变的字符序列;线程安全的,效率低;底层使用char[]存储。

StringBuilder:可变的字符序列;jdk5.0新增的,线程不安全的,效率高;底层使用char[]存储。

 

效率问题:

    /*

    对比String、StringBuffer、StringBuilder三者的效率:

    从高到低排列:StringBuilder > StringBuffer > String

     */

 

posted @ 2020-02-26 15:55  智阿广  阅读(182)  评论(0)    收藏  举报