- String属于引用数据类型,不属于基本数据类型
- 在java中随便使用双引号括起来的都是String对象,例如”Hello World”
- java中规定,双引号括起来的字符串 ,是不可变的,也就是说”Hello World”自出生到死亡,不可变,不能变成”Hello Java”
- 在JDK中双引号括起来的字符串,都是直接存储在方法区的“字符串常量池”中
public class StringTest {
public static void main(String[] args) {
// 创建了3个字符串对象,存储在字符串常量池中
String s1 = "abc";
String s2 = "def";
// new对象的时候,一定在堆内存中开辟空间
String s3 = new String("xjj");
}
}
常用的构造方法:public class StringTest {
public static void main(String[] args) {
// 创建字符串对象最常用的一种方式
String s1 = "def";
// 常用的构造方法
byte[] bytes = {97,98,99};
String s2 = new String(bytes);
// String类已经重写了toString方法
System.out.println(s2);//abc
// String(字节数组,数组元素下标的起始位置,长度)
String s3 = new String(bytes, 1, 2);
System.out.println(s3);//bc
// 将char数组全部转换成字符串
char[] chars = {'我','叫','T','M','D'};
String s4 = new String(chars);
System.out.println(s4);
// 将char数组的一部分转换为字符串
// String(char数组,起始下标,显示长度)
String s5 = new String(chars,2,3);
System.out.println(s5);
}
}
常用方法:public class StringTest {
public static void main(String[] args) {
char c = "我叫TMD".charAt(2);
System.out.println(c);// T
// 按字典顺序比较两个字符串
int index1 = "abc".compareTo("abcd");
int index2 = "abcdef".compareTo("abc");
int index3 = "abc".compareTo("bac");
System.out.println(index1);//-1
System.out.println(index2);//3
System.out.println(index3);//-1
// 当且仅当此字符串包含指定的char值序列时,返回true
System.out.println("Hello Java".contains("Java"));// true
System.out.println("https://www.e1yu.com".contains("http://"));// false
// 判断当前字符串是否以某个字符串结尾
System.out.println("abc.java".endsWith(".java"));//true
System.out.println("abc.java".endsWith(".test"));//false
// 判断两个字符串是否相等,忽略大小写
System.out.println("HELLO WORLD".equalsIgnoreCase("hello world"));//true
// 将字符串对象转化字节数组
byte[] bytes = "abcdef".getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);// 97 98 99 100 101 102
}
// 判断某个子字符串在当前字符串中第一次出现的索引
System.out.println("python#java#c++#mysql".indexOf("java"));//7
}
}
public class StringTest {
public static void main(String[] args) {
// 判断某个字符串是否为"空字符串"
String s1 = "";
System.out.println(s1.isEmpty());// true
// 判断字符串长度时length()方法
System.out.println("abc".length());//3
// 判断某个子字符串在当前字符串中最后一次出现的索引
System.out.println("python#java#mysql#php#java".lastIndexOf("java"));//22
// 字符串替换
String s2 = "http://www.e1yu.com".replace("http","https");
System.out.println(s2);//https://www.e1yu.com
// 字符串拆分
String[] s3 = "2020-08-13".split("-");
for (int i = 0; i < s3.length; i++) {
System.out.println(s3[i]);//2020 08 13
}
// 判断某个字符串是否以某个字符串开始
System.out.println("abc#java#python".startsWith("abc"));//true
// 截取字符串,包含起始下标,不包含结束下标
// 左闭右开
System.out.println("https://www.e1yu.com".substring(8));//www.e1yu.com
System.out.println("https://www.e1yu.com".substring(8,11));//www
}
}
public class StringTest {
public static void main(String[] args) {
// 将字符串转换成char数组
char[] chars = "我叫TMD".toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println(chars[i]);
}
// 转换为小写
System.out.println("Hello WORLD".toLowerCase());//hello world
// 转换为大写
System.out.println("Hello WORLD".toUpperCase());//HELLO WORLD
// 去除字符串前后空白
System.out.println(" hello eyujun ".trim());//hello eyujun
// 将非字符串转换成字符串 静态方法
String s1 = String.valueOf(true);
System.out.println(s1);//字符串true
}
}
声明:1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!如有侵权请邮件联系客服!
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!