Java学习笔记之字符串常用方法

一、String关键字一些常用方法

1、构造方法:

public String(); 空构造

public String(byte[]  bytes);将字节数组转成字符串

public String (byte[] bytes ,int index,int length);将字节数组的一部分转成字符串

public String (char[] value);将字符数组转成字符串

public String (char[] value,int index ,int count);将字符数组的一部分转成字符串

public String (String original);将字符常量转成字符串

2、判断功能

boolean equals(object obj);比较字符串是否相同,区分大小写

boolean equalsIgnoreCase(String str);比较字符串是否相同,不区分大小写

boolean contains(String str);判断字符串是否包含小字符串

boolean startsWith(String str);判断字符串是否以某个字符串开头

boolean endsWith(String str);判断字符串是否以某个字符串结尾

boolean isEmpty();判断字符是否为空

3、获取功能

int length();获取长度

char charAt(int index);获取指定字符的位置

int indexOf(int ch);返回指定字符在此字符串中第一次出现的索引

int indexOf(String str);返回指定字符串在此字符串中第一次出现的索引

int indexOf(int ch,from Index);返回指定字符在指定位置后第一次出现的索引

int indexOf(String str ,from Index);返回指定字符串在指定位置第一次出现的索引

String subString(int start);从指定位置截取字符串到结束

String subString(int start ,int end);从指定位置截取字符串到指定结束位置

4、转换功能

byte[]  getBytes();将字符串转化为字节数组

char[]  toCharArray();将字符串转化为字符

static  String valueOf (char[] chs);就字符转化为字符串

static  String valueOf(int  i);将int类型的值转化为字符串

String toLowerCase();将字符串小写

String toUpperCase();将字符串大写

String concat(String str);字符串拼接

5、替换功能

String replace(char old ,char new);字符替代

String replace(String old ,String new);字符串替换

6、去除空格

String trim();

7、按照字典顺序比较两个字符串

int compareTo(String str);

int compareToIgnoreCase(String str);

8、StringBuffer常用功能

append(String str);可以将任意类型的数据添加到字符串缓冲区里,并返回字符串缓冲区本身

insert(int offset,String str);在指定位置插入任意类型的数据到字符串缓冲区,并返回字符串缓冲区本身

reverse();反转

原文地址:https://www.cnblogs.com/zhoumiao/p/5567075.html