一.字符串
(1)字符串的表示
java.lang.String 代表不可变的字符序列,String用来表示由字符组成的序列,是最常用的一种引用数据类型,另外一个字符串就是一个字符(Char)类型的数组。
(2)字符串的创建
• 标准:
先声明后使用
• 创建方法
(1)直接赋值:String str = "this is a string";
(2)创建实体:String s = new String("this is a string");
(3)用字符型数组创建:String(char a[])
char a[] = {'a','b','c'};
String s = new String(char a[])
(4)用字节型数组创建一个字符串:String(byte[]bytes,int offset,int length)
public class StringTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//1.直接输出
String s = "this is a string";
String s1 = "this is a string";
//2.通过创建实体
String s2 = new String("this is a string");
String s3 = new String("this is a string");
//3.char[]字节型进行传递
char a[] = {'a','b','c'};
String s4 = new String(a);
//4.char[]字符数组取一段输出
String s5 =new String(a,0,3);
//输出(两种对比方式)
System.out.println(s.equals(s1));//true
System.out.println(s==s1);//true
System.out.println(s2==s3);//false
System.out.println(s2.equals(s3));//true
System.out.println(s4==s5);//false
System.out.println(s4.equals(s5));//true
/*summary:
* == 是判断两个是否相同,new创建实体后因为实体不相同,所以不相同
* equals 是判断两个字符串的内容是不是相同,只看内容
* end */
}
}
(3)字符串常用方法(第一等开头去空截取长替结尾分割大小写)
• public char charA