Java字符串:String

原创文章,转载请注明原文章地址,谢谢!

字符串

由字符组成的一串字符序列,称为字符串。而Java中的字符串是由双引号括起来的多个字符。

Java中的字符采用Unicode编码,所以Java字符串可以包含中文等亚洲字符,如:"世界你好"。另外,单个字符如果用双引号括起来,那它表示的是字符串,而不是字符了,如:"A",需要注意的是,字符串还有一个极端情况,就是""表示空字符串,双引号中没有任何内容,空字符串不是null,空字符串是分配内存空间,而null是没有分配内存空间。

JavaSE提供了三种字符串类,分别是String、StringBuffer和StringBuilder。String是不可变字符串,StringBuffer和StringBuilder是可变字符串。

String

在Java中不可变的字符串类是String,属于java.lang包,在Java中应用非常广泛。

提示:java.lang包中提供了很多Java基础类,包括Object、Class、String和Math等基本类。在使用java.lang包中的类时不需要引入(import)该包,因为它是由解释器自动引入的。

String中常用的构造方法
  • String():使用空字符串创建并初始化一个字符串对象。
  • String(String original):使用另一个字符串创建并初始化字符串对象。
  • String(StringBuffer buffer):使用StringBuffer对象创建新的字符串对象。
  • String(StringBuilder builder):使用StringBuilder对象创建新的字符串对象。
  • String(byte[] bytes):使用byte数组创建并初始化字符串对象。
  • String(char[] chars):通过字符数组创建新的字符串对象。
  • String(char[] chars, int offset, int count):通过字符串数组创建字符串对象,offset参数是子数组的第一个字符索引,count参数是指定子数组的长度。
String s1 = new String();
String s2 = new String("hello");
String s3 = new String("\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064");
char[] chars = {'a', 'b', 'c', 'd', 'e'};
String s4 = new String(chars);
String s5 = new String(chars, 1, 4);
byte[] bytes = {97, 98, 99};
String s6 = new String(bytes);
字符串池

我们在Java中创建字符串对象时,可以通过new String来创建,但是我相信在实际应用中,获得字符串对象是直接使用字符串常量,这两种方式都可以,但是其中是有一些区别的。

String s1 = "hello";
String s2 = "hello";
String s3 = new String("hello");
String s4 = new String("hello");
System.out.println(s1 == s2);//true
System.out.println(s2 == s3);//false
System.out.println(s3 == s4);//false

首先明确一点,==是比较两个引用是否指向相同的对象,那么观察上面的代码,s1和s2是指向相同对象,s2,s3,s4指向不同的对象。这是为何?

在Java中,对于字符串常量,是采用字符串池(String pool)管理的。结合上面代码,来解释一下。首先创建s1对象的时候,会在字符串池中找“hello”常量,发现没有,那就创建一个“hello”字符串对象,并放到字符串池中。接下来创建s2,又会去字符串池中找“hello”,发现有,那就将引用指向这个常量。但是在使用new关键字创建对象的时候,每次new的时候,都会新创建一个空间地址,新创建的对象引用会指向这个地址,所以s3和s4的引用是不一样的,它们和s1或s2更不是同一个引用了。
字符串拼接

String字符串虽然是不可变字符串,但也可以进行拼接只是会产生一个新的对象。String字符串拼接可以使用+运算符或String的concat(String str)方法。+运算符优势是可以连接任何类型数据拼接成为字符串,而concat方法只能拼接String类型字符串。

String s1 = "hello";
//拼接String类型
String s2 = s1 + "world";
//拼接int类型
String s3 = s2 + 1;
//拼接Date类型
String s4 = s3 + new Date();
//使用concat拼接
String s5 = s4.concat("你好世界");

上述代码需要注意的是,在进行拼接的时候,每次拼接后的都是一个新的对象。

字符串查找

在String中提供了字符串查找的方法,indexOf和lastIndexOf。返回出现字符串第一次出现的索引,没有则返回-1。

  • int indexOf(int ch):正序查找,返回第一次出现ch所在的索引。
  • int indexOf(int ch, int fromIndex):从指定的索引开始正序查找,返回第一次出现ch所在的索引。
  • int indexOf(String str):正序查找,返回第一次出现str所在的索引。
  • int indexOf(String str, int fromIndex):从指定的索引开始正序查找,返回第一次出现str所在的索引。
  • int lastIndexOf(int ch):倒序查找,返回第一次出现ch所在的索引。
  • int lastIndexOf(int ch, int fromIndex):从指定的索引开始倒序查找,返回第一次出现ch所在的索引。
  • int lastIndexOf(String str):倒序查找,返回第一次出现str所在的索引。
  • int lastIndexOf(String str, int fromIndex):从指定的索引开始倒序查找,返回第一次出现str所在的索引。
  • charAt(int index):返回索引index所在位置的字符。

提示:字符串本质上是字符数组,因此它也有索引,索引从零开始。

String str = "hello world, Java is coming";
//正序
System.out.println(str.indexOf(97));//14
System.out.println(str.indexOf(97, 14));//14
System.out.println(str.indexOf("a"));//14
System.out.println(str.indexOf("a", 23));//-1
//倒序
System.out.println(str.lastIndexOf("a"));//16
System.out.println(str.charAt(0));//h
字符串比较

String提供了一些比较相等、比较大小、比较前后缀等的方法。

  • boolean equals(Object anObject):比较两个字符串中内容是否相等。
  • boolean equalsIgnoreCase(String anotherString):类似equals方法,只是忽略大小写。
  • int compareTo(String anotherString):按字典顺序比较两个字符串。如果参数字符串等于此字符串,则返回值0;如果此字符串小于字符串参数,则返回一个小于0的值;如果此字符串大于字符串参数,则返回一个大于0的值。
  • int compareToIgnoreCase(String str):类似compareTo,只是忽略大小写。
  • boolean endsWith(String suffix):此字符串是否以指定的后缀结束。
  • boolean startsWith(String prefix):此字符串是否以指定的前缀开始。
String s1 = "hello";
String s2 = "world";
String s3 = "WORld";
System.out.println(s1.equals(s2));//false
System.out.println(s2.equalsIgnoreCase(s3));//true
System.out.println(s1.compareTo(s2));//-15
System.out.println(s2.compareToIgnoreCase(s3));//0
System.out.println(s1.startsWith("h"));//true
System.out.println(s1.endsWith("h"));//false
字符串截取
  • String substring(int beginIndex):从指定索引beginIndex开始截取一直到字符串结束的子字符串。
  • String substring(int beginIndex, int endIndex):从指定索引beginIndex开始截取直到索引endIndex处的字符,注意包括索引为beginIndex处的字符,但不包括索引为endIndex处的字符。
String s = "hello world";
System.out.println(s.substring(6));//world
//从w开始,到r结束,但是不包括r
System.out.println(s.substring(6, 8));//wo
字符串分割
  • String[] split(String regex):根据给定正则表达式的匹配拆分此字符串。
  • String[] split(String regex, int limit):根据给定正则表达式的匹配拆分此字符串,limit参数控制模式应用的次数,因此影响所得数组的长度。
String s = "hello,world,a,b,c";
String[] arr = s.split(",");
for (String result : arr) {
    System.out.println(result);
}
//将s切割为两份
String[] arr1 = s.split(",", 2);
for (String result1 : arr1) {
    System.out.println(result1);
}

博客内容仅供自已学习以及学习过程的记录,如有侵权,请联系我删除,谢谢!

你可能感兴趣的:(Java字符串:String)