一、String类的特征
1.String类使用final修饰,没有子类,不可被继承。
2.String类代表字符串。 Java程序中的所有字符串文字(例如"abc" )都被实现为此类的对象。
3.String类就是一个不可变字符串类.
二、String类如何创建对象
1.直接赋值-----将字符串数据值直接赋值给String类型的变量
String str1="hello";
2.通过构造方法创建对象
public class StringClass {
public static void main(String[] args) {
//String是一个类,不可被修改,是被final修饰的,没有子类,不能被继承,是一个不可变的字符串
// 有两种创建方式
//1.直接赋值
String name = "实例变量";//存放于内存堆区的常量池中,只有一个对象
//2.通过String构造方法创建
//存放于内存堆区和常量池中,有两个对象
//表示空字符串
String str = new String();
//String(String original) 初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本。
String str1 = new String("测试");
System.out.println(str1);
//也可以传一个变量
String value = "张三";
String str2 = new String(value);
System.out.println(str2);
//String(byte[] bytes) 通过使用平台的默认字符集解码指定的字节数组来构造新的 String 。
//将字节数组转换成字符串
byte bytearray[] = {78,79,80,81};
String str3 = new String(bytearray);
System.out.println(str3);
System.out.println(str3.length());
//String(char[] value) 分配一个新的 String ,以便它表示当前包含在字符数组参数中的字符序列。
//将字符数组转换成字符串
char chararray[] = {'我','是','好','人'};
String str4 = new String(chararray);
System.out.println(str4);
System.out.println(str4.length());
}
}
三、String类的常用方法
1.char charAt(int index)得到原始字符串中指定位置的字符【位置从0开始】 |
2.String concat(String str)将字符连接起来组成一个新的字符串 |
3.boolean contains(String str)判断指定的字符序列,是否在原始字符串中存在 |
4.boolean endsWith(String suffix)匹配后缀,查看字符串是否以指定后缀结尾 |
5.boolean startsWith(String prefix)匹配前缀,查看字符串是否以指定前缀开头 |
6.byte[] getBytes()将字符串转换成字节数组 |
7.int indexOf(String str)得到指定字符串数据在原始字符串中第一次出现的位置 |
8.int lastIndexOf(String str)得到指定字符串数据在原始字符串中最后一次出现的位置 |
9.boolean isEmpty()判断字符串是否为空,字符串长度为0 |
10.int length() 返回此字符串的长度。 |
11.String replace(String old, String new)用指定的字符串替换就得字符串数据 |
12.String[] split(String regex)将原始字符串通过执行的符号拆分成一个字符串数组 |
13.String substring(int beginIndex)从指定的开始位置截取到字符串结尾,得到一个子字符串 String substring(int beginIndex, int endIndex)从指定的开始位置截取到指定的结束位置,得到一个子字符串 |
14.char[] toCharArray()将字符串转换成字符数组 |
15.String toUpperCase() 将字符串中的字母转换成大写字母 |
16.String toLowerCase()将字符串中的字母转换成小写字母 |
17.String trim()去掉原始字符串中两头的空格 |
public class StringClass1 {
public static void main(String[] args) {
String name = "张云英";
//1.char charAt(int index)得到原始字符串中指定位置的字符【位置从0开始】
//得到name字符串中云的字符
char charbianliang = name.charAt(1);
System.out.println(charbianliang);
System.out.println("---------------------");
//2.String concat(String str)字符串合并,相当于+号
//给name 后面加上",真好看"
String name1 = name.concat(",真好看");
System.out.println(name1);
System.out.println("---------------------");
//3.boolean contains(String str)判断原始字符串中,是否有指定字符
//创建一个字符数组
char chararray[] = {'我','是','高','家','村','的','人'};
String st = new String(chararray);
//System.out.println(st);
System.out.println(st.contains("高"));
System.out.println("---------------------");
//4.boolean endsWith(String suffix)匹配后缀,查看字符串是否以指定后缀结尾
String strarray[] = {"王五","赵柳","王年年","张三丰","李荣华","张华","肖大年"};
for (String value:strarray){
if (value.endsWith("年")){
System.out.println(value);
}
}
System.out.println("---------------------");
//5.boolean startsWith(String suffix)匹配前缀,查看字符串是否以指定前缀开头
for (String val:strarray){
if (val.startsWith("张")){
System.out.println(val);
}
}
System.out.println("---------------------");
//6.byte[] getBytes()将字符串转换成字节数组
String str1 = "w";
byte byarry[] = str1.getBytes();
for (byte val:byarry){
System.out.println(val);
}
System.out.println("---------------------");
//7.int indexOf(String str)得到指定字符串数据在原始字符串中【第一次】出现的位置
String str2 = "我是中国人中中中";
int i = str2.indexOf("中");
System.out.println(i);
System.out.println("---------------------");
//8.int lastIndexOf(String str)得到指定字符串数据在原始字符串中最后一次出现的位置
int a = str2.lastIndexOf("中");
System.out.println(a);
System.out.println("---------------------");
//9.boolean isEmpty()判断字符串是否为空,字符串长度为0
System.out.println(str2.isEmpty());
System.out.println("---------------------");
//10.int length() 返回此字符串的长度。
System.out.println(str2.length());
System.out.println("---------------------");
//11.String replace(String old, String new)用指定的字符串替换就得字符串数据
String str3 = str2.replace("中","美");
System.out.println(str3);
System.out.println("---------------------");
//12.String[] split(String regex)将原始字符串通过执行的符号拆分成一个字符串数组
String str4 = "sdfjkk|dlak|sdf,|asdfk";
String strarry[] = str4.split("\\|");
for (String val:strarry){
System.out.println(val);
}
System.out.println("---------------------");
//13.String substring(int beginIndex)从指定的开始位置截取到字符串结尾,得到一个子字符串
String str5 = str4.substring(5);
System.out.println(str5);
System.out.println("---------------------");
//14.String substring(int beginIndex, int endIndex)从指定的开始位置截取到指定的结束位置,得到一个子字符串
String str6 = str4.substring(3,7);
System.out.println(str6);
System.out.println("---------------------");
//15.char[] toCharArray()将字符串转换成字符数组
char arry[] = str6.toCharArray();
for (char val:arry){
System.out.println(val);
}
System.out.println("---------------------");
//16.String toUpperCase() 将字符串中的字母转换成大写字母
String str7 = str4.toUpperCase();
System.out.println(str7);
System.out.println("---------------------");
//17.String toLowerCase()将字符串中的字母转换成小写字母
System.out.println(str7.toLowerCase());
System.out.println("---------------------");
//18.String trim()去掉原始字符串中两头的空格
String id = " 19932 ";
System.out.println(id.length());
System.out.println(id.trim().length());
}
}
四、String类的类型转换
1.String类型与字节数组之间的转换
String --------> 字节数组 byte[ ] getByte( )方法
public class StringClass2 {
public static void main(String[] args) {
//String类型与字节数组之间的转换
//1.String字符串 转换成字节数组 byte[] getByte(String)
String str = "I love you";
byte byarray[] = str.getBytes();
for (byte val:byarray){
System.out.println(val);
}
System.out.println("------------------------------------");
System.out.println(str.length());
}
}
字节数组 ---------> String String类的构造方法
public static void main(String[] args) {
//String类型与字节数组之间的转换
//.字节数组转换成String字符串 String 构造方法
byte bytearray[] = {97,98,99,100};
String str1 = new String(bytearray);
System.out.println(str1);
System.out.println("------------------------------------");
}
}
//结果:abcd
2.String类型与字符数组间的转换
String ---------> 字符数组 char[ ] toCharArray( )方法
public class StringClass2 {
public static void main(String[] args) {
String str2 = "祖国好美丽";
char charry[] = str2.toCharArray();
for (char val:charry){
System.out.println(val);
}
System.out.println("------------------------------------");
System.out.println(str2.length());
}
}
字符数组 --------> String String类的构造方法
public class StringClass2 {
public static void main(String[] args) {
char chararray[] = {'祖','国','好','美','丽'};
String str3 = new String(chararray);
System.out.println(str2);
}
}
3.String类型与基本数据类型间的转换
基本数据类型 ---------> String类型
//1.String类提供的静态方法valueOf()
boolean flag=true;
String booleamstr=String.valueOf(flag);
double dou=12.5;
String doublestr=String.valueOf(dou);
//2.封装类的toString()方法 【不推荐】
boolean flag=false;
Boolean booObject=new Boolean(flag);
String booleanstr=booObject.toString();
String类型 ---------> 基本数据类型【基本数据类型的封装类】
基本数据类型对应的复合数据类型【java类】,只有基本数据类型才有封装类,封装类总共有8种
基本数据类型 | 封装类 |
byte | Byte【类名】 |
short | Short【类名】 |
int | Integer【类名】 |
long | Long【类名】 |
float | Float【类名】 |
double | Double【类名】 |
char | Character【类名】 |
boolean | Boolean【类名】 |
基本数据类型转换成其对应的封装类类型--自动装箱
double doublevalue=168.5; //基本类型
//直接赋值--自动装箱
Double douObject1=doublevalue; //自动装箱
Double douObject2=179.8; //自动装箱
int inval=1001; //基本类型
Integer integer1=inval; //自动装箱
Integer integer2=1008; //自动装箱
//通过封装类的构造方法---自动装箱
boolean boo=true; //基本类型
Boolean booObject=new Boolean(boo); //自动装箱
封装类类型转换基本数据类型-----------自动拆箱
//封装类对象【封装类类型】转换成与之对应的基本数据类型----自动拆箱
Integer integerObject=new Integer(1999); //封装类对象
int intvalue=integerObject; //封装类对象转换成基本类型--自动拆箱
String类型转换成基本类型数据--通过对应基本类型的封装类静态方法
parse对应封装类(String)
String intstring="1010"; //字符串类型
int intval=Integer.parseInt(intstring); //基本类型
System.out.println("intval*100=="+(intval*100)); //101000
String doublestr="168.5"; //字符串
double douvalue=Double.parseDouble(doublestr); //基本类型
System.out.println("douvalue*100=="+(douvalue*100)); //16850.0
五、int 与 Integer有什么区别?
int 是基本数据类型,没有可供操作的变量和方法,只在内存栈区存在资源分配
Integer是int的封装类,是一个java操作类,有提供变量和方法,属于复合数据类型,运行时在内存的堆区和栈区都有资源分配。