一.String类
-
对String在内存存储方面的理解: 第一:字符串一旦创建不可变。 第二:双引号括起来的字符串存储在字符串常量池中。 第三:字符串的比较必须使用equals方法。 第四:String已经重写了toString()和equals()方法
-
String的构造方法。 String s = "abc"; String s = new String("abc"); String s = new String(byte数组); String s = new String(byte数组, 起始下标, 长度); String s = new String(char数组); String s = new String(char数组, 起始下标, 长度);
二.String的常用方法
1.public char charAt(int index)
返回指定索引处的值
例:
String s="我是中国人" ;
System.out.println(s.charAt(3));//国
2.public int compareTo(String anotherString)
(比较两个字符串的大小如果相等返回0 如果前面大返回1 后面大返回 -1)
例:
int result="abcd".compareTo("abcd");
System.out.println(result);//0
int result1="abce".compareTo("abcd");
System.out.println(result1);//1
int result2="abcd".compareTo("abce");
System.out.println(result2);//-1
返回:
3.public boolean contains(CharSequence s)//CharSequence 是一个接口
当且仅当此字符串包含指定的char值序列时才返回true。
例:
System.out.println("http\\www.baidu.com".contains("http"));
//true
System.out.println("http\\www.baidu.com".contains("https"));
//flase
4.public boolean endsWith(String suffix)
测试此字符串是否以指定的字符串结尾。
例:
System.out.println("http\\www.baidu.com".endsWith("om"));
//true
System.out.println("http\\www.baidu.com".endsWith("cm"));
//flase
5.equals (字符串相等比较不忽略大小)返回值boolean
equals的重写:
public boolean equals(Object obj) {
if(obj==null ||!(obj instanceof Animal))
return false;
if(this==obj)
return true;
Animal animal=(Animal)obj;
return animal.no==this.no&&animal.name.equals(this.name)&&animal.birthday.equals(this.birthday);
}
6.public boolean equalsIgnoreCase(String anotherString)
如果两个字符串的长度相同,并且两个字符串中的相应字符等于忽略大小写,则两个字符串被认为是相等的。
例:
System.out.println("ABCD".equalsIgnoreCase("abcd"));//true
7.public byte[] getBytes(String charsetName)
将字符串对象转换成字节数组
例:
byte [] bytes="abcde".getBytes();
for(int i=0;i<bytes.length;i++) {
System.out.println(bytes[i]);
}
8.public int indexOf(String str)
返回指定子字符串第一次出现在字符串内的索引。
例:
System.out.println("abcdefghhgfedcba".indexOf("abc"));//0
9.public boolean isEmpty()
isEmpty 判断某个字符串是否为空字符串 返回值为boolean (字符串的长度也可用length()判断长度)
例:
System.out.println("abcdefghhgfedcba".isEmpty());//false
10.length() 判断字符串的长度 返回值为int
System.out.println("abcdefghhgfedcba".length());//16
public int lastIndexOf(String str)
lastIndexOf 返回指定子字符串最后一次出现在字符串内的索引。
System.out.println("abcdefghhgfedcba".lastIndexOf("cba"));
//13
public String replace(CharSequence target,CharSequence replacement)将老的字符串的子串替换为一段新的字符串例:
String newString="abccdef".replace("bcc", "adde");
System.out.println(newString);//aaddedef
String newString1="ab=ccd=ef".replace("=", ";");
System.out.println(newString1);//ab;ccd;ef
13.将字符串按指定的字符进行拆分
例:
String []newString1="ab=ccd=ef".split("=");//按"="进行拆分
for(int i=0 ;i<newString1.length;i++) {
System.out.println(newString1[i]);
}
结果:
14.subString
public String substring(int beginIndex)
返回一个字符串,该字符串是此字符串的子字符串。 子字符串以指定索引处的字符开头,并扩展到该字符串的末尾。
例:
String s="abcededfefafafa".substring(5);
System.out.println(s);//edfefafafa
public String substring(int beginIndex,int endIndex)
返回一个字符串,该字符串是此字符串的子字符串。 子串开始于指定beginIndex并延伸到字符索引endIndex - 1 。 因此,子串的长度为endIndex-beginIndex
例:
String s="abcededfefafafa".substring(5,8);
System.out.println(s);//edf 前包后闭 [5,8)
15.public boolean startsWith(String prefix)
测试此字符串是否以指定的前缀开头
System.out.println("http\\www.baidu.com".startsWith("om"));
//false
System.out.println("http\\www.baidu.com".startsWith("http"));
//true
16.public char[] toCharArray()
将此字符串转换为新的字符数组。
例:
char[]s="我是中国人".toCharArray();
for(int i=0;i<s.length;i++) {
System.out.println(s[i]);
}
17.public String toLowerCase() 转换为小写
例:
String s="abDcfeD".toLowerCase();
System.out.println(s);//abdcfed
18.public String toupperCase() 转换为小写
String s="abDcfeD".toLowerCase();
System.out.println(s);//ABDCFED
19.public String trim()
去除字符串的前后空格
String s=" hello word! ".trim();
System.out.println(s);//hello word!
20.valueOf
将非字符串转换为字符串
System.out.println(String.valueOf(true));//true
System.out.println(String.valueOf(100));//100
System.out.println(String.valueOf(3014));//3014
System.out.println(String.valueOf('a'));//a
三.String StringBuffer和StringBuilder
1.String为什么是不可变的?
String类中有一个byte[]数组 ,这个byte[]数组采用了final修饰,因为数组一旦创建长度不可变。并且被final修饰的引用一且指向某个对象之后 ,不可再指向其它对象,所以String是不可变的!
"abc"无法变成"abcd ”
2、StringBuilder/StringBuffer为什么是可变的呢?
StringBuffer/StringBuilder内部实际上是一个byte[ ]数组,这个byte[ ]数组没有被final修饰, StringBuffer/StringBuilder的初始化容量为16,当存满之后会进行扩容,底层调用了数组拷贝的方法
System. arraycopy()…是这样扩容的。所以stringBuilder/StringBuffer适合于使用字符串的频繁拼接操作。