java.lang.String类代表字符串
API中说明,Java程序中的所有字符串字面值(如“abc”)都作为此类的实例实现。
就是说:程序当中所有的双引号字符串都是String类的对象。(就算是没有new,也是)
字符串特点:
1.字符串内容永不可变。
2.字符串可以共享使用。
3.字符串效果相当于char[]字符数组,但是底层原理都是byte[]字节数组。
创建字符串3+1种方式:
三种构造方法:
public String():创建一个空白字符串,不含任何内容。
// 无参构造
String str = new String();
public String(char[] array):根据字符数组的内容,来创建对应的字符串
// 通过字符数组构造
char chars[] = {'a' , 'b' , 'c'};
String str2 = new String(chars) ;
public String(byte[] array):根据字节数组的内容,来创建对应的字符串
// 通过字节数组构造
byte bytes[] = {97, 98, 99};
String str3 = new String(bytes) ;
一种直接创建:
//直接创建
String str = "Hello";
字符串常量池:
程序当中直接写上双引号的字符串,就在字符串常量池中。
对于基本类型来说,==是进行数值的比较。
对于引用类型来说,==是进行【地址值】的比较。
从JDK1.7开始,在堆内存中有一个部分叫做字符串常量池,字符串常量池中保存的对象时指向byte[]字节数组的地址值。
字符串常用方法:
equals()
据上述,引用类型的==是进行对象的地址值比较,如果确实需要进行字符串内容的比较,可以使用两个方法:
public boolen equals(Object obj):参数可以是任何对象,只有参数是一个字符串并且内容相同的才会返回true,否则返回false。
public boolean equalsIgnoreCase (String anotherString) : 将此字符串与指定对象进行比较, 忽略大小写。
注意事项:
1.任何对象都能用Object接收。
2.equals方法具有对称性,也就是说a.equals(b)和b.equals(a)效果相同。
3.如果比较双发一个常量一个变量,推荐吧常量字符串写在前面。
推荐:“abc”.equals(str)
不推荐:str.equals("abc")
解释:字符串常量“abc”.equals(null)返回false,null.equals("abc")返回空指针异常NullPointerException。
public class String {
public static void main(String[ ] args) {
// 创建字符串对象
String s1 = "hello";
String s2 = "hello";
String s3 = "HELLO";
// boolean equals(Object obj) : 比较字符串的内容是否相同
System.out.println(s1.equals(s2) ) ; // true
System.out.println(s1.equals(s3) ) ; // false
System.out.println("‐‐‐‐‐‐‐‐‐‐‐") ;
//boolean equalsIgnoreCase(String str) : 比较字符串的内容是否相同, 忽略大小写
System.out.println(s1. equalsIgnoreCase(s2) ) ; // true
System.out.println(s1. equalsIgnoreCase(s3) ) ; // true
System.out.println("‐‐‐‐‐‐‐‐‐‐‐") ;
}
}
length()
public int length():获取字符串当中含有的字符个数,拿到字符串长度。
String s = "hello world";
s.length();
concat()
public String concat(String str):将当前字符串和参数字符串拼接并返回新的字符串。
String s = "hello world";
String s2 = s.concat("Java");
charAt()
public char charAt(int index):获取指定位置的单个字符。
String s = "hello world";
s.charAt(0);
indexOf()
public int indexOf(String str)查找参数字符串在本字符串当中首次出现的索引位置,如果没有返回-1值。
String s = "hello world";
s.indexOf("e");
substring()
public String substring(int beginIndex, int endIndex):返回一个子字符串,从beginIndex到endIndex截取字符串,左闭右开。
String s = "hello world";
s.substring(3, 8);
toCharArray()
public char[ ] toCharArray () : 将此字符串转换为新的字符数组。
getBytes()
public byte[ ] getBytes () : 使用平台的默认字符集将该 String编码转换为新的字节数组。
replace()
public String replace (CharSequence target, CharSequence replacement) : 将与target匹配的字符串使
public class String {
public static void main(String[] args) {
//创建字符串对象
String s = "abcde";
// char[] toCharArray() : 把字符串转换为字符数组
char[] chs = s.toCharArray() ;
for(int x = 0; x < chs. length; x++) {
System.out.println(chs[x] ) ;
}
System.out.println("‐‐‐‐‐‐‐‐‐‐‐") ;
// byte[] getBytes() : 把字符串转换为字节数组
byte[] bytes = s.getBytes() ;
for(int x = 0; x < bytes.length; x++) {
System. out. println(bytes[ x] ) ;
}
System. out. println("‐‐‐‐‐‐‐‐‐‐‐") ;
// 替换字母it为大写IT
String str = "itcast itheima";
String replace = str.replace("it", "IT") ;
System.out.println(replace) ; // ITcast ITheima
System.out.println("‐‐‐‐‐‐‐‐‐‐‐") ;
}
}
split()
public String[] split(String regex):将此字符串按照给定的regex(规则)拆分为字符串数组。
public String{
public static void main(String[] args){
//创建字符串对象
String s = "aa|bb|cc";
String[] strArray = s.split("|");
for (int x = 0; x < strArray.length; x++){
System.out.println(strArray[x]);
}
}
}