java学习第六课:常用API(零基础)

一、String--字符串类

package test3;

//1.String--字符串类
//实例化
public class test19 {
	public static void main(String[] args) {
		String s = "hello world";
		String s1 = new String("hello world");
		String s11 = new String("HELLO WORLD");
		// 1.1 实例化,构造方法的参数可以传递 字符数组,字节数组
		byte b[] = { 65, 66, 67, 68 };// 字节数组
		String s2 = new String(b);// 字节数组转换成字符串,对应ASCII值
		char c[] = { 'a', 'b', 'c', 'd' };// 字符数组
		String s3 = new String(c);
		System.out.println(s);//输出
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s3);
		// 1.2 字符串比较 equals(严格区分大小写) equalsIgnoreCase(不区分大小写)
		boolean b1 = s.equals(s1);// true 因为是小写
		boolean b2 = s.equals(s11);// false 因为是大写
		boolean b3 = s.equalsIgnoreCase(s11);// true 因为不区分大小写
		System.out.println(b1);
		System.out.println(b2);
		System.out.println(b3);

	}
}

1.3字符串的拼接

package test3;

public class test20 {
	public static void main(String[] args) {
		// 1.3 String 增加方法--字符串的拼接 +
		String s = "hello";
		String s1 = "world";
		String s2 = "!";
		String s3 = s + s1 + s2;// s与s1与s2拼接形成一个新的字符串
		// 注:s与s1与s2字符串本质上没有改变,只是形成一个新的字符串
		System.out.println(s3);
		System.out.println(s1);//s1仍为world

		// 另外一种拼接方法-concat
		s = s.concat(s1);// 将s1与s拼接
		// concat拼接不改变s或s1字符串内容,二十产生一个新的字符串返回
		// 所以可知拼接方法不改变元字符串内容
		System.out.println(s);// 输出helloworld
		System.out.println(s1);// 但是s1不改变,仍未world

	}
}

1.4字符串删除

package test3;

public class test21 {
	public static void main(String[] args) {
		// 1.4 String 删除方法 截取子串
		String s = "hello world tom and jerry";
		//          0123456789ABCDEFGHIJKLMNO--所以可知从0开始
		String s1 = s.substring(5);// 从5号位置开始截取(包括5号位置),截取到句尾,截取后的结果返回新字符串

		String s2 = s.substring(5, 8);// 从5号位置开始截取(包括5号位置),截取到8号位置结束(不包括8号位置)
		System.out.println(s);
		System.out.println(s1);
		System.out.println(s2);
	}
}

1.5修改字符,字符串

package test3;

public class test22 {
	public static void main(String[] args) {
//1.5 String类 修改方法 replace-修改某一个字符
		String s = "hello world.";
		// .变为!,h变为H
		s = s.replace('.', '!');// 替换完成后会产生一个新字符串
		System.out.println(s);//输出hello world! 由hello world.--hello world!
		s = s.replace('h', 'H');// 替换完成后会产生一个新字符串
		System.out.println(s);//Hello world!

		// 修改一个长字符串的方法 replaceALL 旧的字符串替换为一个新的字符串,新旧字符串长度不需要一致
		String s1 = "tom and jerry and tom sal hello world hi tom and jerry";
		s1 = s1.replaceAll("tom", "TOM");// 如果需要替换的字符串需要替换多次,用all,全部进行替换
		System.out.println(s1);//TOM and jerry and TOM sal hello world hi TOM and jerry

		s1 = s1.replaceFirst("and", "AND");// 如果需要替换的字符串出现多次,但是只需要替换第一个出现的,用first
		System.out.println(s1);//TOM AND jerry and TOM sal hello world hi TOM and jerry

	}
}

1.6字符串查找

package test3;

public class test23 {
	public static void main(String[] args) {
		// 1.6 字符串查找方法 indexOf
		// s.indexOf(String s1) 查询s1字符串在s中第一次出现的位置
		String s = "hello world and tom and jerry 4 and 89";
		         // 0123456789 123456789 123456789 123456789 
		// 注:所查字符串的位置都是第一个字符所在的位置
		System.out.println(s.indexOf('4'));// 查找4这个字符在字符串s中的位置,如未查找到则返回-1
		System.out.println(s.indexOf("an"));// 查找字符串an,第一次在s这个字符串中出现的位置
		System.out.println(s.indexOf("an", 13));// 查找字符串an,从13号位置开始第一次出现的位置

		// s.lastIndexOf(String s1) 从后往前,查询s1字符串在s中第一次出现的位置
		System.out.println(s.lastIndexOf("an"));
		System.out.println(s.lastIndexOf("an", 23));// 从后往前,从23号开始查an的位置

		// charAt(i)可以查找i号位置的元素
		System.out.println(s.charAt(6));
	}
}

1.7字符串分割方法

package test3;

import java.util.Scanner;

public class test24 {
	public static void main(String[] args) {
		// 1.7 String类 字符串分割方法
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();// 张三 1号 20
		System.out.println(s);//输入三个字符串,中间用下面的进行分割,由下面可知为空格分割
		String s1[] = s.split(" ");// 代表以什么字符串做分割,返回一定会是一个字符串数组
		System.out.println(s1[0]);//输出第一个空格分割前的第一个字符串数组-张三
		System.out.println(s1[1]);//输出第二个字符串数组-1号
		System.out.println(s1[2]);//输出低三个字符串数组-20
		Student st = new Student(s1[0], s1[1], s1[2]);

	}
}

class Student {
	String name;
	String id;
	String age;

	Student(String name, String id, String age) {
		//前一个name为Employee或者说类的属性,而后一个name则指的是一个函数的一个参数(变量,比如张三李四)
		//而后面这个函数的参数就是用于给前一个类的属性赋值的,或者说后面的name就是当前对象的name要修改的值
		this.name = name;//指这个名字现在变成输入的名字,以此类推
		this.id = id;
		this.age = age;

	}

}

二、StringBuilder类(可变字符串)

package test3;

public class test25 {
	public static void main(String[] args) {
		// 2. StringBuilder类-可变字符串(几乎所有String类的方法,StringBuilder类都可以用)
		// 注;所有方法都会直接生效在字符串本身上,与之前形成一个新的字符串不同
		// 2.1实例化
		String s = "hello world";
		StringBuilder a = new StringBuilder(s);
		System.out.println(a);
		// 2.2字符串的追加 append
		a.append("! and hi world! and tom and jerry");// a直接完成追加内容
		System.out.println(a);
		// 2.3字符串的删除 delete
		a.delete(2, 5);// 从2号位置开始删除(包括2号位置),删除到5号位置(不包括5号位置)
		System.out.println(a);
		a.deleteCharAt(0);// deleteCharAt-删除某个指定位置的字符
		System.out.println(a);
		// 2.4字符串的替换 replace
		a.replace(2, 6, "AAAADDDDDDbbbbb");// 从2号位置(包含),到6号位置(不包含),全部替换为指定字符
		// 注意,如果超出位置范围,仍会塞进字符串中
		System.out.println(a);
	}
}

三、本节题目

【问题描述】

编写程序将一串文本中所有指定字符串删除,并返回删除后的文本内容及指定删除字符串最后一次出现的位置

【输入形式】

一串字符串文本

指定删除的字符串

【输出形式】

删除后内容

删除字符串位置

【样例输入】

从标准输入中输入要删除的字符串:

dsdsdfalfklkfsionlsfksdlfskfjskdjfl
in
【样例输出】
dsdsdfalfklkfsionlsfksdlfskfjskdjfl
-1
【样例输入】
dsdsdfalfklkfsiinlsfksdlfskfjskdjfl
in
【样例输出】
dsdsdfalfklkfsilsfksdlfskfjskdjfl
15
【样例输入】
dsfafjalfjinaljfwoelinalfalqqfsinlajlf
in
【样例输出】
dsfafjalfjaljfwoelalfalqqfslajlf
31
【样例输入】
dsfafjalfjinaljfwoelinalfalqqfsinlajlf
faf
【样例输出】
dsjalfjinaljfwoelinalfalqqfsinlajlf
2
【样例输入】
dsfafjalfjinaljfwoelinalfalqqfsinlajlf
weo
【样例输出】
dsfafjalfjinaljfwoelinalfalqqfsinlajlf
-1

package test3;

import java.util.Scanner;

public class test26 {
	public static void mian(String[] args) {
		Scanner sc = new Scanner(System.in);
		String a = sc.nextLine();
		String b = sc.nextLine();
		System.out.println(a.lastIndexOf(b));// 查找最后出现位置
		a = a.replaceAll(b, "");
		System.out.println(a);// 删除后字符串
/*package test;
import java.util.Scanner;
public class test15 {
	public static void main(String []args) {
		Scanner sc=new Scanner(System.in);
		String longStr =sc.nextLine();
		String shortStr =sc.nextLine();
		
		int lastIndex =longStr.lastIndexOf(shortStr);
		System.out.println(lastIndex);
		
		longStr =longStr.replaceAll(shortStr, "");
		System.out.println(longStr);
	*/

	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值