0
点赞
收藏
分享

微信扫一扫

2022年8月5日——Java中String类的使用(1)

描述:

Java中String的常用方法的使用

案例展示:

1

2022年8月5日——Java中String类的使用(1)_字符串

2

2022年8月5日——Java中String类的使用(1)_指定位置_02

代码演示:

1

字符数组转字符串

package test.day_02;


import java.io.UnsupportedEncodingException;

public class Demo1 {

public static void main(String[] args) throws UnsupportedEncodingException {
// 字符数组
byte[] bytes = {97,98,99,100,101};
// 将字符数组,以指定的编码格式进行转化为字符串
String s = new String(bytes,"utf-8");
System.out.println(s);
}
}

2

根据指定的位置获取指定的字符

判断字符串中是否包含有指定的字符

在字符串的末尾添加字符

package test.day_02;

public class Demo2 {
public static void main(String[] args) {
String str = "hello World";
// 获取指定位置的字符
System.out.println(str.charAt(0));
// 获取指定位置的字符的Unicode数值
System.out.println(str.codePointAt(0));
System.out.println(str.codePointAt(1));
// 获取指定位置的字符的Unicode数值之前的数据
System.out.println(str.codePointBefore(1));
// 将字符添加到字符串末尾
System.out.println(str.concat(" YEAR "));
// 判断字符串中是否包含指定的字符
System.out.println(str.contains("年"));
}
}


举报

相关推荐

0 条评论