4.21学习内容——String类

1.所有的文本都是字符串,字符串随处可见##

2.创建字符串

1.引用字符串常量


public class StringText {

	public static void main(String[] args) {
		String a = "刘情利是个小傻子";
		String b = "没错,他就是一个小傻子";
		String c = "说的太对了",d = "说的非常对";
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		System.out.println(d);
		
	}
}

2.利用构造方法直接实例化


public class StringText {

	public static void main(String[] args) {
		String a = new String("刘情利不白");
		String b = new String(a);
		System.out.println(a);
		System.out.println(b);
	}
}

3.利用字符数组实例化


public class StringText {

	public static void main(String[] args) {
		char[] charArray = {'l','q','l'};
		String a = new String(charArray);
		System.out.println(a);
		String b = new String(charArray,1,2);
		System.out.println(b);
	}
}

4.利用字节数组实例化


public class StringText {

	public static void main(String[] args) {
		byte[] byteArray = {-60,-61,-3,-4};
		String a = new String(byteArray);
		System.out.println(a);
	}
}

3.字符串的拼接

1.使用“+”运算符进行拼接


public class StringText {

	public static void main(String[] args) {
		String a = "刘情利"+"是傻子";
		String b = a + ",说的对";
		System.out.println(a);
		System.out.println(b);
	}
}

2.使用“+=”运算符进行拼接


public class StringText {

	public static void main(String[] args) {
		String a = "123"+"456"+"789";
		String b = "123";
		b += "456";
		b += "789"+"123";
		System.out.println(a);
		System.out.println(b);
		String therepublicpeopleofchina = "中华";
		therepublicpeopleofchina += "人民共和国";
		System.out.println(therepublicpeopleofchina);		
	}
}

+=主要应用于变量名较长时,+主要应用于连接的字符比较多时

4.获取字符串的长度

int x = a.length();

包含空格

5.判断子字符串是否存在


public class StringText {

	public static void main(String[] args) {
		String a = "钱钱钱钱钱钱够";
		String b = "狗狗狗狗狗钱";
		if(a.indexOf("够") > -1) {
			System.out.println("a有够");
		}
		
		if(b.indexOf("钱") > -1) {
			System.out.println("b有钱");
		}
		System.out.println(a.indexOf("钱"));//判断的是第一次出现的位置
	}
}

6.获取指定位置的字符

char c = a.charAt(1);

7.获取字符出现时的索引

int x1 = a.indexOf("钱");
int x2 = a.indexOf("钱", 3);//从指定位置开始往后查
int y1 = a.lastIndexOf("钱");
int y2 = a.lastIndexOf("钱", 4);//指定位置往前查
System.out.println(x1+" "+x2+" "+y1+" "+y2);

8.获取指定位置的字符串


public class StringText {

	public static void main(String[] args) {
		String a = "112336555547899874";
		String x = a.substring(6);
		System.out.println(x);
		
		String x1 = a.substring(6,14);//开始以及结束位置
		System.out.println(x1);
	}
}

9.去除字符串中的空白内容(指定内容的替换)


public class StringText {

	public static void main(String[] args) {
		String a = "00 2 15243 5  23 12 532 4  ";
		String b = a.replaceAll("\\s","");
		System.out.println(b);
		
		String c = b.replaceAll("4","0");
		System.out.println(c);
	}
}

10.判断字符串开始、结尾内容


public class StringText {

	public static void main(String[] args) {
		String a = "HelloWorld.java";
		String b = "宋刘";
		boolean x = a.endsWith(".java");
		boolean y = b.startsWith("刘");
		System.out.println(x);
		System.out.println(y);
	}
}

11.判断字符串是否相等


public class StringText {

	public static void main(String[] args) {
		String a = "abc";
		String b = "abc";
		String c = new String("abc");
		String d = new String(a);
		String e = "ABC";
		System.out.println(a.equals(b));
		System.out.println(a == d);//用字符串常量创建的指向一个
		System.out.println(a == b);
		System.out.println(a.equalsIgnoreCase(e));
		System.out.println(a.equals(e));
	}
}

在这里插入图片描述

12.字符串的大小写转换

    d.toLowerCase();//转换小写
	d.toUpperCase();//转换大写

13.字符串的分割


public class StringText {

	public static void main(String[] args) {
		String a = "192.168.0.1";
		String[] str1 = a.split("\\.");
		String[] str2 = a.split("\\.", 3);//分割成的部分
		
		for(int i = 0;i < str1.length;i++) {
			System.out.print("["+str1[i]+"]");
		}
		
		System.out.println();
		
		for(int i = 0;i < str2.length;i++) {
			System.out.print("["+str2[i]+"]");
		}
	}
}

运行结果

14.时间与日期的字符串格式化

import java.util.Date;

public class StringText {

	public static void main(String[] args) {
		Date date = new Date();
		System.out.println(date);
		String a = String.format("%tF",date);
		System.out.println(a);
	}
}

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

15.常规类型的格式化


public class StringText {

	public static void main(String[] args) {
		System.out.printf("%.6f",Math.PI);
		System.out.println();
		System.out.println(String.format("%e",120000000.1));
	}
}

在这里插入图片描述

16.正则表达式

判断是不是手机号的代码

import java.util.Scanner;

public class shoujihao {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String tele = "(13[0-9]|15[012356789]|18[056789])\\d{8}";
		while(true) {
			String t = in.nextLine();
			if(t.matches(tele)) {
				System.out.println("是一个手机号码");
			}
			else {
				System.out.println("不是手机号");
			}
		}
	}
}

在这里插入图片描述

在这里插入图片描述在这里插入图片描述在这里插入图片描述

17.StringBuffer类


public class StringBufferText {

	public static void main(String[] args) {
		StringBuffer a = new StringBuffer("abcd");
		a.append("efghidk");//追加字符
		System.out.println(a);
		a.setCharAt(0,'x');//修改指定地方的字符
		System.out.println(a);
		StringBuffer b = a.reverse();//逆序输出
		System.out.println(b);
		StringBuffer c = a.delete(0, 5);//删除字符,5之前的
		System.out.println(c);
	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值