String常用方法(一)
/*
* 方法系列一:
* (1)int length():返回字符串的长度,返回的是字符的个数
* 因为String的内部(JDK1.9之前)用char[]实现,这个长度就是value数组的长度
* (2)boolean isEmpty():是否是空字符串
* (3)String toLowerCase()
* String toUpperCase()
* (4)String trim() :去掉字符串的前后空白符
* (5)String concat():拼接,等价于+
*/
public class TestStringMethod01 {
@Test
public void test3(){
String s1 = "hello";
String s2 = "world";
String s3 = s1 + s2;
String s4 = s1.concat(s2);
System.out.println(s3 == s4);//false
System.out.println(s3.equals(s4));//true
}
@Test
public void test2(){
String str = " hello world ";
str = str.trim();
System.out.println("[" + str + "]");
}
@Test
public void test1(){
String str = "hello";
System.out.println(str.toUpperCase());
}
}
String常用方法(二)
/*
* 方法系列二:和char相关
* (1)char[] toCharArray()
* (2)char charAt(index)
* (3)String(char[] arr)
* (4)String(char[] arr,int offset, int count)
*/
public class TestStringMethod2 {
@Test
public void test05(){
char[] arr = {'h','e','l','l','o'};
String string = new String(arr,2,3);//从[2]开始,取3个
System.out.println(string);
}
@Test
public void test04(){
char[] arr = {'h','e','l','l','o'};
String string = new String(arr);
System.out.println(string);
}
@Test
public void test03(){
String str = "HelloWorld";
System.out.println(str.charAt(4));
}
@Test
public void test02(){
Scanner input = new Scanner(System.in);
System.out.print("请输入性别:");
String str = input.next();
char gender = str.charAt(0);
System.out.println(gender);
}
@Test
public void test01(){
String str = "HelloWorld";
//统计大写字母的个数
char[] arr = str.toCharArray();
int count = 0;
for (int i = 0; i < arr.length; i++) {
if(arr[i]>='A' && arr[i]<='Z'){
count++;
}
}
System.out.println("大写字母的个数:" +count);
}
}
String常用方法(三)
/*
* 方式系列三:和byte相关,或者说和编码与解码相关
* (1)byte[] getBytes():编码的方法,使用平台默认的字符编码进行编的
* byte[] getBytes(编码方式):
*
* 编码:对于ASCII码范围内(0~127),无论用什么编码方式,结果都是一样的,一个字符对应一个字节的编码值
* 对于其他的字符,编码,结果不一定是几个字节,例如汉字:
* UTF-8:变长的,但是大多数汉字都是3个字节
* GBK、GB2312:固定2个字节
* ISO8859-1:不支持中文,所有字符都变为1个字节
* (2)new String(字节数组)
* new String(字节数组,编码方式)
*
* 编码:
* 把字符-->字节的过程,编给计算机用的
* 解码:
* 把字节-->字符的过程,解给人看的
*
* 乱码:
* (1)编码与解码字符集不一致
* (2)缺字节
*/
public class TestStringMethod3 {
@Test
public void test05() throws UnsupportedEncodingException{
String str = "尚硅谷";
byte[] bytes = str.getBytes("ISO8859-1");
System.out.println(Arrays.toString(bytes));//[63, 63, 63]
String string = new String(bytes,"ISO8859-1");
System.out.println(string);
}
@Test
public void test04() throws UnsupportedEncodingException{
String str = "尚硅谷";
byte[] bytes = str.getBytes("GBK");
System.out.println(Arrays.toString(bytes));//[-55, -48, -71, -24, -71, -56]
String string = new String(bytes,"GBK");
System.out.println(string);
}
@Test
public void test03() throws UnsupportedEncodingException{
String str = "尚硅谷";
byte[] bytes = str.getBytes();
System.out.println(Arrays.toString(bytes));//[-27, -80, -102, -25, -95, -123, -24, -80, -73]
String string = new String(bytes,"UTF-8");
System.out.println(string);
}
@Test
public void test02(){
String str = "abc";
byte[] bytes = str.getBytes();
System.out.println(Arrays.toString(bytes));//[97, 98, 99]
}
@Test
public void test01(){
String str = "a";
byte[] bytes = str.getBytes();
System.out.println(Arrays.toString(bytes));//[97]
}
}
String常用方法(四)
/*
* 方法系列(4):
* (1)boolean startsWith(xx)
* (2)boolean endsWith(xx)
*/
public class TestStringMethod4 {
@Test
public void test3(){
String fileName = "Hello.class";
if(fileName.endsWith(".java")){
System.out.println("Java的源文件");
}else if(fileName.endsWith(".class")){
System.out.println("字节码文件");
}
}
@Test
public void test2(){
String str = "尚硅谷是一家靠谱的培训机构";
System.out.println(str.startsWith("尚硅谷"));
}
@Test
public void test1(){
String name = "崔志恒";
if(name.startsWith("张")){
System.out.println("张家人");
}else{
System.out.println("不是张家人");
}
}
}
String常用方法(五)
/*
* 方法系列(5):和查找有关
* (1)是否包含
* boolean contanis
* (2)int indexOf(xx):如果存在返回下标,如果不存在返回-1
* (3)int lastIndexOf(xx):如果存在返回最后一个的下标,如果不存在返回-1
*/
public class TestStringMethod5 {
@Test
public void test03(){
String fileName = "Hello.java.txt";
//文件的后缀名是什么
//截取文件的后缀名
//(1)第一步,找到最后一个.的位置
int index = fileName.lastIndexOf(".");
System.out.println(index);
}
@Test
public void test02(){
String str = "123.45";
// String str = "123";
int index = str.indexOf(".");
System.out.println(index);
}
@Test
public void test01(){
String str = "123.45";
if(str.contains(".")){
System.out.println("是小数");
}
}
}
String常用方法(六)
/*
* 方法系列(6):截取
* (1)String substring(int beginIndex):从字符串的[beginIndex]截取到最后
* (2)String substring(int beginIndex, int endIndex):截取字符串的[beginIndex,endIndex)部分
*/
public class TestMethod6 {
@Test
public void test04(){
String str = "helloworldjava";
String sub = str.substring(2, 6);
System.out.println(sub);
}
@Test
public void test03(){
String fileName = "Hello.java.txt";
//文件的后缀名是什么
//截取文件的后缀名
//(1)第一步,找到最后一个.的位置
int index = fileName.lastIndexOf(".");
//(2)截取
String sub = fileName.substring(index);
System.out.println(sub);
}
}
String常用方法(七)
/*
* 方法系列(7):匹配规则
* boolean matches(正则表达式)
*
* 正则表达式:用于检测文本的格式
* 校验某个字符串是否符合xx规则
* 例如:电话号码
* 甚至可以校验是否是移动号...
* 银行卡号
* 邮箱格式
* ....
*
*
*/
public class TestMethod7 {
@Test
public void test2(){
String str = "12a345";
//简单判断是否全部是数字,这个数字可以是1~n位
//正则不是Java的语法,它是独立与Java的规则
//在正则中\是表示转义,
//同时在Java中\也是转义
boolean flag = str.matches("\\d+");
System.out.println(flag);
}
@Test
public void test1(){
String str = "123456789";
//判断它是否全部由数字组成,并且第1位不能是0,长度为9位
//第一位不能是0,那么数字[1-9]
//接下来8位的数字,那么[0-9]{8}+
boolean flag = str.matches("[1-9][0-9]{8}+");
System.out.println(flag);
}
}
String常用方法(八)
/*
* 方法系列(8):替换
* (1)String replace(target, value)
* (2)String replaceAll(String regex, String replacement)
* (3)String replaceFirst(String regex, String replacement)
* 其中(2)和(3)支持正则
*/
public class TestMethod8 {
@Test
public void test4(){
String str = "hello244world.java;887";
//把其中的非字母去掉
str = str.replaceAll("[^a-zA-Z]", "");
System.out.println(str);
}
@Test
public void test3(){
String str = "Hello World World";
str = str.replaceAll("World", "***");
System.out.println(str);
}
@Test
public void test2(){
String str = "Hello World";
str = str.replaceFirst("World", "***");
System.out.println(str);
}
@Test
public void test1(){
String str = "Hello World";
str = str.replace("World", "***");
System.out.println(str);
}
}
String常用方法(九)
/*
* 方法系列(9):拆分
* String[] split(xx)
*/
public class TestMethod9 {
@Test
public void test4(){
String str = "张三.23|李四.24|王五.25";
//|在正则中是有特殊意义,我这里要把它当做普通的|
String[] all = str.split("\\|");
//转成一个一个学生对象
Student[] students = new Student[all.length];
for (int i = 0; i < students.length; i++) {
//.在正则中是特殊意义,我这里想要表示普通的.
String[] strings = all[i].split("\\.");//张三, 23
String name = strings[0];
int age = Integer.parseInt(strings[1]);
students[i] = new Student(name,age);
}
for (int i = 0; i < students.length; i++) {
System.out.println(students[i]);
}
}
@Test
public void test3(){
String str = "1Hello2World3java4atguigu5";
str = str.replaceAll("^\\d|\\d$", "");
String[] all = str.split("\\d");
for (int i = 0; i < all.length; i++) {
System.out.println(all[i]);
}
}
@Test
public void test2(){
String str = "1Hello2World3java4atguigu";
str = str.replaceFirst("\\d", "");
System.out.println(str);
String[] all = str.split("\\d");
for (int i = 0; i < all.length; i++) {
System.out.println(all[i]);
}
}
@Test
public void test1(){
String str = "Hello World java atguigu";
String[] all = str.split(" ");
for (int i = 0; i < all.length; i++) {
System.out.println(all[i]);
}
}
}
class Student{
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}