String类到底是什么
有些同学学完Java却还没搞清楚String到底是什么,有什么用,什么时候用,还是一知半解,我给大家罗列了一些String常见的一些方法,以及怎么样去使用,因为String这东西是面试必考题也比较基础,经常会遇到。1.String类概述
A:"abc"是String类的一个实例,或者成为String类的一个对象
B:字符串字面值"abc"也可以看成是一个字符串对象
C:字符串是常量,一旦被赋值,就不能被改变
D:字符串本质是一个字符数组
2.String类的构造方法
String(String original):把字符串数据封装成字符串对象
String s =New String(“dasdasdasf”);
String(char[] value):把字符数组的数据封装成字符串对象
char[] value = {‘a’,’b’,’c’,’d’};
String s1 = new String(value);
String(char[] value, int index, int count):把字符数组中的一部分数据封装成字符串对象
String s2= new String(value,1,2);
3.String类的构造方法代码演示
package com.gh_02;
/*
* String:字符串类
* 由多个字符组成的一串数据
* 字符串其本质是一个字符数组
*
* 构造方法:
* String(String original):把字符串数据封装成字符串对象
* String(char[] value):把字符数组的数据封装成字符串对象
* String(char[] value, int index, int count):把字符数组中的一部分数据封装成字符串对象
*
* 注意:字符串是一种比较特殊的引用数据类型,直接输出字符串对象输出的是该对象中的数据。
*/
public class StringDemo {
public static void main(String[] args) {
//方式1
//String(String original):把字符串数据封装成字符串对象
String s1 = new String("hello");
System.out.println("s1:"+s1);
System.out.println("---------");
//方式2
//String(char[] value):把字符数组的数据封装成字符串对象
char[] chs = {'h','e','l','l','o'};
String s2 = new String(chs);
System.out.println("s2:"+s2);
System.out.println("---------");
//方式3
//String(char[] value, int index, int count):把字符数组中的一部分数据封装成字符串对象
//String s3 = new String(chs,0,chs.length);
String s3 = new String(chs,1,3);
System.out.println("s3:"+s3);
System.out.println("---------");
//方式4
String s4 = "hello";
System.out.println("s4:"+s4);
}
}
如果还是不太明白,看了下面这张图会更好理解一点
4.通过构造方法创建的字符串对象和直接赋值方式创建的字符串对象有什么区别呢?
通过构造方法创建字符串对象是在堆内存。
直接赋值方式创建对象是在方法区的常量池。
==:
基本数据类型:比较的是基本数据类型的值是否相同
引用数据类型:比较的是引用数据类型的地址值是否相同
下面是代码示例:
public class StringDemo2 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = "hello";
System.out.println("s1:"+s1);
System.out.println("s2:"+s2);
System.out.println("s1==s2:"+(s1==s2)); //false
String s3 = "hello";
System.out.println("s1==s3:"+(s1==s3)); //false
System.out.println("s2==s3:"+(s2==s3)); //true
}
}
5.String类的判断功能
boolean equals(Object obj):比较字符串的内容是否相同 boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写 boolean startsWith(String str):判断字符串对象是否以指定的str开头boolean endsWith(String str):判断字符串对象是否以指定的str结尾
6.String类获取功能
/*
* 遍历字符串(获取字符串中的每一个字符)
*/
public class StringTest {
public static void main(String[] args) {
//创建一个字符串对象
String s = "abcde";
//原始做法
System.out.println(s.charAt(0));
System.out.println(s.charAt(1));
System.out.println(s.charAt(2));
System.out.println(s.charAt(3));
System.out.println(s.charAt(4));
System.out.println("---------");
//用for循环改进
for(int x=0; x<5; x++) {
System.out.println(s.charAt(x));
}
System.out.println("---------");
//用length()方法获取字符串的长度
for(int x=0; x<s.length(); x++) {
System.out.println(s.charAt(x));
}
}
}
6.String类转换功能
- String类的转换功能:
- char[] toCharArray():把字符串转换为字符数组
- String toLowerCase():把字符串转换为小写字符串
- String toUpperCase():把字符串转换为大写字符串
字符串的遍历:
A:length()加上charAt()
B:把字符串转换为字符数组,然后遍历数组
public class StringDemo {
public static void main(String[] args) {
//创建字符串对象
String s = "abcde";
//char[] toCharArray():把字符串转换为字符数组
char[] chs = s.toCharArray();
for(int x=0; x<chs.length; x++) {
System.out.println(chs[x]);
}
System.out.println("-----------");
//String toLowerCase():把字符串转换为小写字符串
System.out.println("HelloWorld".toLowerCase());
//String toUpperCase():把字符串转换为大写字符串
System.out.println("HelloWorld".toUpperCase());
}
}
6.String类其他功能
去除字符串两端空格 String trim()
按照指定符号分割字符串 String[] split(String str)
public class StringDemo {
public static void main(String[] args) {
//创建字符串对象
String s1 = "helloworld";
String s2 = " helloworld ";
String s3 = " hello world ";
System.out.println("---"+s1+"---");
System.out.println("---"+s1.trim()+"---");
System.out.println("---"+s2+"---");
System.out.println("---"+s2.trim()+"---");
System.out.println("---"+s3+"---");
System.out.println("---"+s3.trim()+"---");
System.out.println("-------------------");
//String[] split(String str)
//创建字符串对象
String s4 = "aa,bb,cc";
String[] strArray = s4.split(",");
for(int x=0; x<strArray.length; x++) {
System.out.println(strArray[x]);
}
}
}
关于String的方法真的是太多了,在这里根本写不完,大家可以查API,里面列举了所有String的功能及如何使用,还有其他很多关键字的用法,多看多查多学,一定能学会。