String 概述
String类在java.lang包下,所以使用的时候不需要导包。
String类代表字符串,Java程序中的所有字符串文字(例如“abc”)都被实现为此类的实例。也就是说,Java程序中所有的双引号字符串,都是String类的对象。
字符串的特点:
字符串不可改变,它们的值创建后不能被更改。
虽然String的值是不可改变的,但是它们可以被共享。
字符串效果上相当于字符串数组(char[]),但是底层原理是字节组(byte[]).
String 构造方法
| 方法名 | 说明 |
| public String() | 创建一个空白字符串对象,不含有任何内容。 |
| public String(char []chs ) | 根据字符组数的内容,来创建字符串对象 |
| public String(byte[] bys) | 根据字节数组的内容,来创建字符串对象 |
| String s = “abc” | 直接赋值的方式创建字符串对象,内容是abc |
代码演示:
package Strings;
import java.lang.String;
public class Strings {
public static void main(String[] args) {
//创建一个空白字符串对象,不含有任何内容。
String s1 = new String();
System.out.println("S1:" + s1);
// 根据字符组数的内容,来创建字符串对象
char[] chs = {'a', 'b', 'c' };
String s2 = new String(chs);
System.out.println("s2:" + s2);
//根据字节数组的内容,来创建字符串对象
byte[] bys = {97, 98, 99};
String s3 = new String(bys);
System.out.println("s3:" + s3);
//直接赋值的方式创建字符串对象,内容是abc
String s = "abc";
System.out.println("s:" + s);
}
}
String对象的特点:
(1)通过new创建字符对象,每一次new都会申请一个内存空间,虽然内容相同,但是地址值不同。
char [] chs= {"a","b","c"};
String s1= new String(chs);
String s2= new String(chs);
上面代码,JVM会首先创建一个字符数组,然后每一次new的时候都会有一个新的地址,只不过s1和s2参考的字符串内容是相同的。
字符串的比较:
使用==作比较
基本数据类型:比较的是数据值是否相同;
引用数据类型:比较的是地址值是否相同。
字符串是对象,它比较内容是否相同,是通过一个方法来实现的,这个方法叫:equals()
public Boolean eaqals(Obiect anObject):将此字符串与指定的对象进行比较,由于我们比较的字符串对象,所以参数直接传递体格字符串。
package Strings;
public class StringQulas {
public static void main(String[] args) {
char[] chs = {'a', 'b', 'c'};
String s1 = new String(chs);
String s2 = new String(chs);
String s3 = "abc";
String s4 = "abc";
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
System.out.println(s3 == s4);//true
System.out.println("----------");
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s3.equals(s4));
}
}
字符串的基本操作:
(1)length() 返回字符串的长度。
(2)substring() 从一个下标开始到另一个下标的子字符串。
(3)toUpperCase() 将字符串转为大写,tolowerCase() 将字符串转为小写。
(4)trim() 返回删除了前后空白的字符串。
(5)i sEmpty() 判断字符串是否为空,返回值是Boolean类型.
(6) concat()将两个字符串连接起来,产生一个新的字符串
(7) replace()改变字符串中的某一个字符
(8) charAt() 返回字符串中指定的字符。
package demo;
public class Stringbasics {
public static void main(String[] args) {
//字符串的基本操作
String str = " Zhengsanming is a computer operator!! ";
String str1 = "This is young man !!";
// length() 返回字符串的长度。
System.out.println(str.length());
//substring() 从一个下标开始到另一个下标的子字符串。
System.out.println(str.substring(19, 28));
//toUpperCase() 将字符串转为大写
System.out.println(str.toUpperCase());
//tolowerCase() 将字符串转为小写。
System.out.println(str.toLowerCase());
//trim() 返回删除了前后空白的字符串。
System.out.println(str.trim());
// isEmpty() 判断字符串是否为空,返回值是Boolean类型.
System.out.println(str.isEmpty());
// 将两个字符串连接起来,产生一个新的字符串。
System.out.println(str.concat(str1));
// replace()改变字符串中的某一个字符
System.out.println(str.replace('a', 'A'));
// charAt() 返回字符串中指定的字符。
System.out.println(str.charAt(20));
}
}
将字符串转换为数组
有两种方法:
(1)toCharArray()方法,将一个字符串,转化为字符数组。
(2)getChars(srcBegin,srcEnd,char [] dst,dstBegin);srcBegin代表字符串的开始下标索引,srcEnd代表字符串结束下标索引。char [] dst代表目标数组,dstBegin代表目标数组开始的位置。
package com.zhengsanming.String;
public class aeeString {
public static void main(String[] args) {
String s = "Thsis is Geely University of China. Welcome to Computer science and technology class 8.";
char[] chars = s.toCharArray();
System.out.println(chars);
char[] subs = new char[10];
s.getChars(10, 20, subs, 0);
System.out.println(subs);
}
}
字符串的拆分与组合
使用String类的split()方法可以将一个字符串分解为子字符串或令牌(token),使用join()方法可以将String中的字符产连接起来。
package com.zhengsanming.String;
public class strSpilt {
public static void main(String[] args) {
String s = "Thsis is Geely University of China. Welcome to Computer science and technology class 8.";
String[] str = s.split(" ");
for (String ss : str) {
System.out.println(ss);
}
String joined= String.join("/","Thsis is"," Geely University of China. ","Welcome to Computer science and ","technology class 8.");
System.out.println(joined);
String [] siji={"春","夏","秋","冬"};
System.out.println(String.join("-",siji));
}
}
格式化输出:%d(十进制整数)、%f(十进制浮点数)、%e(科学计数法)、%s(字符串)、%b(布尔值)、%c(Unicode)、%n(换行),下面给大家具体演示一下。
package com.zhengsanming.String;
public class strformat {
public static void main(String[] args) {
// %d
System.out.printf("Year=|%6d|%n", 2017);
//%f
System.out.printf("%8.3f%n", 27.1314520);
//%e
System.out.printf("%10.3e%n", 27.1314520);
//%c
byte b = 65;
System.out.printf("%8c%n", b);
//%b
byte bs = 0;
String s = null;
System.out.printf("b1=%b%n", bs);
System.out.printf("b2=%b%n", true);
System.out.printf("b3=%b%n", s);
}
}

被折叠的 条评论
为什么被折叠?



