Author:老九
计算机专业
可控之事 沉重冷静 不可控之事 乐观面对
85180586@qq.com
😄 😆 😵 😭 😰 😅 😢 😤 😍 ☺️ 😎 😩
👍 👎 💯 👏 🔔 🎁 ❓ 💣 ❤️ ☕️ 🌀 🙇 💋 🙏 💦 💩 ❗️ 💢
————————————————
版权声明:本文为CSDN博主「浦上青天」的原创文章
文章目录
知识点
创建字符串
常见构造String方式
1.直接赋值
String str1 = "hello";
2.构造方法
String str2 = new String("hello");
要注意的是:String str2 = str1; 并不是 str2 引用了 str1 ,而是指向了和 str1 一样的地址。所以,修改 str1 的值,str2 的值不受影响:
public static void main(String[] args) {
String str1 = "abcdef";
String str2 = str1;
System.out.println(str1);
System.out.println(str2);
str1 = "Lockey";
System.out.println(str1);
System.out.println(str2);
}
字符串比较相等
如果是两个 int 类型的话,可以直接用 == 比较,但如果是两个字符串类型的话,就有区别了:
public static void main(String[] args) {
String str1 = "hello";
String str2 = "hello";
System.out.println(str1 ==str2);
}
public static void main(String[] args) {
String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println(str1 == str2);
}
第一种是将值存在字符串常量池,第二种是在堆中开辟了两个新的空间
String 使用 == 比较并不是在比较字符串内容, 而是比较两个引用是否是指向同一个对象。
字符串不可变性
字符串是不可改变的,就算使用 + 也只是改变了字符串的引用,就像下面这样:
public static void main(String[] args) {
String str = "hello" ;
str = str + " world" ;
str += "!!!" ;
System.out.println(str);
}
它的结果只是指向了最后一个字符串。
字符,字节,字符串的转换
字符与字符串
将字符数组转化为字符串
通过 new 一个字符串完成转化:
public static void main(String[] args) {
char[] val = {'a','b','c','d','e','f'};
String str = new String(val);
System.out.println(str);
}
当然 new 的时候也可以选择从某个位置到某个位置创建字符串:
public static void main(String[] args) {
char[] val = {'a','b','c','d','e','f'};
String str1 = new String(val,1,3);
System.out.println(str1);
}
将字符串变成字符
public static void main(String[] args) {
String str2 = "hello";
char ch = str2.charAt(2);
System.out.println(ch);
}
当然,也能把整个字符串变成字符数组:
public static void main(String[] args) {
String str2 = "hello";
char[] ch1 = str2.toCharArray();
System.out.println(Arrays.toString(ch1));
}
字节与字符串
将字节变成字符串
还是通过 new 一个字符串来完成,而且也可以从某个位置完成字符串的转化:
public static void main(String[] args) {
byte[] bytes = {97,98,99,100};
String str = new String(bytes);
System.out.println(str);
String str2 = new String(bytes,1,3);
System.out.println(str2);
}
字符串变成字节
通过将字符串转化为字节数组来完成:
public static void main(String[] args) {
String str = "abcdef";
byte[] bytes = str.getBytes();
System.out.println(Arrays.toString(bytes));
}
字符串常见操作
字符串比较
String str1 = "hello" ;
String str2 = "Hello" ;
System.out.println(str1.equals(str2)); // false
System.out.println(str1.equalsIgnoreCase(str2)); // true
System.out.println("a".compareTo("A")); // 32
字符串查找
1.contains():判断一个字符串是否存在
String str = "helloworld" ;
System.out.println(str.contains("world")); // true
2.indexOf()方法进行位置查找
String str = "helloworld" ;
System.out.println(str.indexOf("world")); // 5,w开始的索引
System.out.println(str.indexOf("cool")); // -1,没有查到
if (str.indexOf("hello") != -1) {
System.out.println("可以查到指定字符串!");
}
3.判断开头或结尾:
String str = "**@@helloworld!!" ;
System.out.println(str.startsWith("**")); // true
System.out.println(str.startsWith("@@",2)); // ture
System.out.println(str.endwith("!!")); // true
字符串替换
方法:replaceAll,replaceFirst
String str = "helloworld" ;
System.out.println(str.replaceAll("l", "_"));
System.out.println(str.replaceFirst("l", "_"));
注意字符串是不可变对象,替换不修改当前字符串,而是产生一个新的字符串
字符串拆分
方法:split()
String str = "hello world " ;
String[] result = str.split(" ") ; // 按照空格拆分
for(String s: result) {
System.out.println(s);
}
字符串截取
方法:subString
String str = "helloworld" ;
System.out.println(str.substring(5));
System.out.println(str.substring(0,5));
其他操作
1.trim(),去掉字符串中左右空格,保留中间空格
String str = " hello world " ;
System.out.println("["+str+"]");
System.out.println("["+str.trim()+"]");
2.isEmpty()方法:判断是否为空字符串
System.out.println("hello".isEmpty());
System.out.println("".isEmpty());
System.out.println(new String().isEmpty());
3.reverse()方法:逆序字符串
public static void main(String[] args) {
StringBuilder s = new StringBuilder("abcdef");
s.reverse();
System.out.println(s);
StringBuffer s1 = new StringBuffer("12345");
s1.reverse();
System.out.println(s1);
}
StringBuilder、StringBuffer
直接赋值
public static void main(String[] args) {
StringBuilder s = new StringBuilder("abcdef");
System.out.println(s);
}
append拼接
public static void main(String[] args) {
StringBuilder s1 = new StringBuilder();
s1.append("abcdef");
s1.append("123");
s1.append("456").append("789");
System.out.println(s1);
}
转化为 String
这里的转化为 String ,StringBuffer 和 StringBuilder 都是使用 toString 方法
public static String func2() {
StringBuffer s = new StringBuffer();
return s.toString();
}
StringBuffer和StringBuilder区别
StringBuffer 比 StringBuilder 多了一个关键字 synchronized 保证线程的安全
在多线程的情况下 用 StringBuffer 会更安全 如果是单线程 就用 StringBuilder
异常
运行时异常
除以 0
public static void main(String[] args) {
System.out.println(10/0);
}
因为 0 不能做除数,所以会报异常:
数组下标越界
public static void main(String[] args) {
int[] arr = {1,2,3};
System.out.println(arr[9]);
}
因为数组的下标最大是 2 ,这里是访问下标 9 ,所以会越界。
编译时异常
使用类克隆的时候,如果不抛出异常的话,就会报错导致出现无法完成编译:
class Person implements Cloneable{
public int id;
}
public class Main{
public static void main(String[] args) {
Person person = new Person();
Person person1 = (Person) person.clone();
}
}
如果重写克隆方法,并且抛出异常的话,程序就可以运行了。
class Person implements Cloneable{
public int id;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Main{
public static void main(String[] args) throws CloneNotSupportedException {
Person person = new Person();
Person person1 = (Person) person.clone();
}
}
捕获异常
通过 try catch 来捕获并且处理异常,代码如下:
try{
有可能出现异常的语句 ;
}[catch (异常类型 异常对象) {
} ... ]
[finally {
异常的出口
}]
try 代码块中放的是可能出现异常的代码.
catch 代码块中放的是出现异常后的处理行为.
finally 代码块中的代码用于处理善后工作, 会在最后执行.
其中 catch 和 finally 都可以根据情况选择加或者不加.
异常体系
自定义异常
在自定义抛出异常的时候,用 throw 来抛出异常。
public static void func4(int x) {
if(x == 10){
System.out.println(10 / x);
throw new RuntimeException("x == 10");
}
}
public static void main(String[] args) {
try {
func4(10);
} catch (ArithmeticException e) {
e.printStackTrace();
}
}
先赞后看,养成习惯!!!^ _ ^♥♥♥
每天都更新知识点哦!!!
码字不易,大家的支持就是我坚持下去的动力。点赞后不要忘记关注我哦!