1.String 构造器

  • public String():
  • public String(byte[] bytes):把字节数组转成字符串
  • public String(byte[] bytes,int index,int length):把字节数组索引位置index,长度为length的字节数组转为字符串
  • public String(char[] value):把字符数组转成字符串
  • public String(char[] value,int index,int count):把字符数组索引位置index,长度为length的字节数组转为字符串
  • public String(String S):把字符串常量S值转成字符串

2.String的判断方法

  • boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
  • boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
  • boolean contains(String str):判断字符串中是否包含str,本质equals方法
  • boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
  • boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
  • boolean isEmpty():判断字符串是否为空。

3.String类获取

  • int length():获取字符串的长度。
  • char charAt(int index):获取指定索引位置的字符
  • int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
  • int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
  • int indexOf(int ch,int index):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
  • int indexOf(String str,int index):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
  • String substring(int start):从指定位置开始截取字符串,默认到末尾。
  • String substring(int start,int end):从指定位置开始直到索引 endIndex - 1 处的字符

4.String的转换功能:

  • byte[] getBytes():把字符串转换为字节数组。
  • char[] toCharArray():把字符串转换为字符数组。
  • static String valueOf(objet o):把任意类型的数据转成字符串。
  • String toLowerCase():把字符串转成小写。
  • String toUpperCase():把字符串转成大写。
  • String concat(String str):把字符串拼接。

5.String替换功能:

  • String replace(char old,char new)把old字符转为new字符
  • String replace(String old,String new)把old字符串转为new字符串
  • String trim()//去除字符串首尾空格

6.String比较功能

  • int compareTo(String str)按字典顺序比较两个字符串。返回一个负整数、0 或一个正整数。
  • int compareToIgnoreCase(String str)按字典顺序比较两个字符串,不考虑大小写。分别返回一个负整数、0 或一个正整数。

7.String 的一些注意点:

  • 字符串如果是变量拼接,先申请空间,在拼接
  • 字符串如果是常量相加,先拼接,然后在常量池找,如果有就直接返回,否则,就申请空间。
  • ==:比较对象的地址值
  • equals:有重写对象的equals方法,比较的是对象内容,否则比较的是对象的地址值
posted on 2017-04-02 23:10  2637282556  阅读(109)  评论(0编辑  收藏  举报