1.注意点
- java中用双引号括起来的都是String对象,且字符串是不可变的
- java中用双引号括起来的字符串都是存储在“方法区”的“字符串常量池”中。字符串在实际开发中使用频繁,所以放在其中
- String s = “abc”; s引用中保存了字符串对象在字符串常量池中的内存地址
- String类已经重写了toSting和equals方法
String s1 = "abcdef";
String s2 = s1+"xy";
String s3 = new String("xy");
String s1 = "hello";
String s2 = "hello";
//s1 == s2 true 双等号比较的是内存地址
String x = new String("xyz");
String y = new String("xyz");
//x == y false 字符串对象之间的比较应该调用equals方法
String k = new String("test");
System.out.println("test".equals(k));//建议使用这种方式,避免空指针异常(null.equals("test"))
String s1 = new String("hello");
String s2 = new String("hello");
//一共创建了3个对象
2.String类的构造方法
- String s = new String("");
- String s = “”;
- String s = new String(char数组);
- String s = new String(“char数组,起始下标,长度”);
- String s = new String(“byte数组”);
- String s = new String(“byte数组,起始下标,长度”);
char[] chars = {"中","国","人"};
String char1 = new String(chars);//中国人
String char2 = new String(chars,0,2);//中国
byte[] bytes = {97,98,99};
String byte1 = new String(bytes);//abc
String byte2 = new String(bytes,0,1);//a
3.String类的常用方法
//1.char charAt(int index)
char c = "中国人".charAt(1);//国
//2.int compareTo(String anotherStrng)
int result1 = "abc".compareTo("abc");// 0
int result1 = "abc".compareTo("abe");// -1
int result1 = "bc".compareTo("ab");// 1
//3.boolean contains(CharSequence s)
System.out.println("HelloWorld".contains("or"));//true
System.out.println("HelloWorld".contains("java"));//false
//4.boolean endsWith(String suffix)
System.out.println("HelloWorld".endWith("orld"));//true
System.out.println("HelloWorld".endWith("java"));//false
//5. boolean equals(Object anObject)
System.out.println("HelloWorld".equals("java"));//false
//comparTo 可以比较大小
//6.boolean equalsIgnoreCase(String anotherString)
System.out.println("abC".equalsIgnoreCase("ABC"));//true
//7.byte[] getBytes()
byte[] bytes = "abcdefg".getBytes();
for(int i =0;i<bytes.length;i++){
System.out.println(bytes[i]);
}
//8.int indexOf(String str)
System.out.println("HelloWorldJava".indexOf("e"));//1
//9.boolean isEmpty()
String s1 = "";
String S2 = "a";
System.out.println(s1);//true
System.out.println(s2);//false
//10.int length()
//判断数组长度是length属性,判断字符串长度是length()方法
System.out.println("abc".length());//3
//11.String[] split(String regex)
String[] ymd = "1980-10-11".split("-");
for(int i = 0;i<ymd.length;i++){
System.out.println(ymd[i]);
}
//12.String replace(CharSequence target,CharSequence replacement)
String newString = "http://www.baidu.com".replace("http:","https:");
System.out.println(newString);//https://www.baidu.com
//13.int lastIndexOf()
System.out.println("abcdefghijk".lastIndexOf(i));//8
//14.boolean startWith(String prefox)
System.out.println("http://www.baidu.com".startWith("http"));//true
//15.String substring(int beginIndex)
System.out.println("http://www.baidu.com".substing(7));//www.baidu.com
//16.String substing(int beginIndex,int endIndex)
System.out.println("http://www.baidu.com".substing(7,10));//www
//起始位置包括,结束位置不包括
//17.char[] toCharArray()
char[] chars = "中国人".toCharArray();
for(int i = 0;i<chars.length;i++){
System.out.println(char[i]);
}
//18.String toLowerCase()
System.out.println("ABCDefg".toLowerCase());//abcdefg
//19.Srting toUpperCase()
System.out.println("abcdEFg".toUpperCase());//ABCDFG
//20.String trim();
SYstem.out.println(" Hello World ".trim())//Hello World
//21.静态方法 String.valueOf(),将非字符串装换为字符串。参数为对象时,底层调用toString方法
String s1 = String.valueOf(100);
class Customer{
public String toString(){
return "我是VIP客户";
}
}
String s1 = String.valueOf(new Customer());
System.out.println(s1);//我是VIP客户
//本质上,System.println()方法输出任何数时,都是先转换为字符串String.ValueOf()再输出。
//输出一个引用时,ValueOf()先执行toSting()方法
传送门
上一章:JavaSE进阶 第六章 数组
下一章:JavaSE进阶 第七章 常用类 (二) StringBuffer与StringBuilder