11.29字符串的常用方法

一、字符与字符串

序号

方法

类型

描述

1

public String(char[] value)

构造方法

表示把字符数组转换为字符串

2

public String(char[] value,

              int offset,

              int count)

构造方法

表示把字符数组转换为字符串,指定字符数组的其实位置,和字符个数

3

public char charAt(int index)

普通方法

表示返回指定索引的字符

4

public char[] toCharArray()

普通方法

表示返回字符数组

二、字符串比较

序号

方法

类型

描述

1

public boolean equals(String anObject)

普通方法

比较字符串内容,区分小大写

2

public boolean equalsIgnoreCase(String anotherString)

普通方法

比较字符串内容,不区分大小写

3

public int compareTo(String anotherString)

普通方法

比较字符串的大小

 

三、字符串查找

序号

方法

类型

描述

1

public boolean contains(String s)

普通方法

是否包含子串

2

public int indexOf(String str)

普通方法

不建议使用,了解

3

public boolean startsWith(String prefix)

普通方法

是否以某个子串开始

4

public boolean endsWith(String suffix)

普通方法

是否以某个子串结

四、字符串替换

序号

方法

类型

描述

1

public String replace(char oldChar,

                      char newChar)

普通方法

表示把旧的子串替换为新的子串

 

2

public String replaceAll(String regex,

                         String replacement)

普通方法

替换全部

 

3

public String replaceFirst(String regex,

                           String replacement)

普通方法

替换第一个

 

五、字符串截取

序号

方法

类型

描述

1

public String substring(int beginIndex)

普通方法

表示从指定索引位置开始截取

2

public String substring(int beginIndex,

 int endIndex)

普通方法

表示从指定开始索引位置和结束索引位置

 

六、字符串拆分

序号

方法

类型

描述

1

public String[] split(String regex)

普通方法

按照指定的字符拆分

2

public String[] split(String regex,

int limit)

普通方法

按照指定的字符拆分,有限制条件

 

七、其他常用方法

序号

方法

类型

描述

1

public String concat(String str)

普通方法

字符串相加,“+”

2

public String toUpperCase()

普通方法

字符串转大写

3

public String toLowerCase()

普通方法

字符串转小写

4

public String trim()

普通方法

去掉字符串两边的空格,中间的不会去掉

5

public int length()

普通方法

求字符串的长度

6

public boolean isEmpty()

普通方法

判断字符串是否为空

7

public String intern()

普通方法

手工入池(常量池)

posted on 2017-11-30 16:37  bingo1717  阅读(140)  评论(0编辑  收藏  举报