String:
表示字符串常量,在创建之后只是不可以被改变的。
1. 一些常用的基础方法:
String(byte[] bytes) :讲字节数转换成字符串public String(byte[] bytes,int index,int length);将字节数组的一部分转换成字符串
public String(char[] value);将字符数组转换成字符串 将字符串转化为字符数组 toCharArray
public String(char[] value,int index,int length);将字符数组的一部分转换成字符串
public String(String original) 将一个字符串常量构造成字符串
public int length()返回此字符串的长度
面试题:(重点)
数组,字符串,集合中有没有length()?
数组中没有length方法,属于属性,带括号的叫方法;
字符串中有 length()方法;
集合里面没有length(), 获取集合的元素数:size()
注释:只有字符串里面有length(),其他都是length属性。
字符串最大的特点:字符串一旦被赋值,其值不能发生改变
面试题:
String s1="abc"
String s2=new String("abc");
这两个分别创建了几个对象?第一个创建了一个: 只在方法区
第二个创建了两个: 方法区以及堆内存
2.String一些常用的判断方法:
boolean equals(Object obj);将字符串与指定的对象相比较
boolean equalsIgnoreCase(String str);将此String与另一个String相比较,不考虑大小写
boolean contains(String str);判断当前大字符串中是否包含子字符串。(重点)
boolean startsWith(String str);以str字符串开头(重点)
boolean endswith(String str);以str字符串结尾(重点)
boolean isEmpty();判断字符串是否为空
boolean concat();字符串的特有功能,吧、拼接功能和+拼接复功能一样
3.String中一些常用的操作方法:
public int length() 返回此字符串的长度。
public char charAt(int index) 返回指定索引处的 char 值
public int indexOf(int ch)返回指定字符在此字符串中第一次出现处的索引
public int indexOf(int ch,int fromIndex)
返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
public int indexOf(String str)返回指定子字符串在此字符串中第一次出现处的索引
public int indexOf(String str,int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
public String substring(int beginIndex)截取字符串,默认从此字符串到末尾。
public String substring(int beginIndex,int endIndex)截取字符串,默认从指定开始到指定结尾。
学习总结:
String类中有许多方法,要多加练习,才能有更深刻的记忆。而且对于String类的创建对象内存知识要多加学习。