面向对象基础#高频的、必会的-字符串常用方法

【日常总结】

​在实际后台开发中,字符串和集合的运用还是比较多,昨天我对String类StringBuffer类StringBuilder类做个简要的总结,用几个demo夯实其基础的应用,对面试中出现的高频点也进行了说明。在今天凌晨1点弄排版、定稿,终于发布出去,还是很不简单。万事开头难,还是有道理的!现在资料、视频铺天盖地,其实很难尽下心来学习,挣扎几番,决定支棱起来,从基础做起,归纳学习起来,毕竟身边的大佬、大腿太多,显然我功力还是不够,学习、成长的东西太多。

​ 我写博客,目的很明确,一是归纳完善技术知识体系,二是提高语言文字能力。鸡汤太多,点到为止,今天说说我要分享的知识要点,接下来主要对字符串常用的方法进行汇总以及代表性的方法进行编码说明,多话不说,看招!

【分享要点】
【1】字符串–常规方法
【2】字符串–查找方法
【3】字符串–截取方法
【4】字符串–字符相关方法
【5】字符串–开头与结尾方法
【6】字符串–替换方法
【7】字符串–拆分方法
说明:字符串的编码与解码、正则匹配涉及的知识点,在后面的分享我会专门的进行总结说明!我列举了五个应用案列,希望真正想了解字符串方法的朋友耐心的看完,一定会有所收获。
【要点总结】

1.字符串常规方法

​ (1)boolean isEmpty():字符串是否为空

​ (2)int length():返回字符串的长度

​ (3) String concat(String str): (拼接)将指定字符串连接到此字符串的结尾

​ (4)boolean equals(Object obj):比较字符串是否相等,区分大小写

​ (5)boolean equalsIgnoreCase(Object obj):比较字符串是否相等,区分大小写

​ (6)int compareTo(String other):比较字符串大小,区分大小写,按照Unicode编码值比较大小

​ (7)int compareToIgnoreCase(String other): 按字典顺序比较两个字符串,不考虑大小写

​ (8)String toLowerCase():将字符串中大写字母转为小写

​ (9)String toUpperCase():将字符串中小写字母转为大写

​ (10)String trim(): 返回字符串的副本,忽略前导空白和尾部空白,(即去掉字符串前后空白符)

01.案例应用
public class Test001 {
	@Test
	public void test01() {
		Scanner input = new Scanner(System.in);
		String word;
		while (true) {
			System.out.print("请输入单词");
			word = input.nextLine();
			/*System.out.println();
			System.out.println("单词"+word.trim());在这里面正确打印*/
			if (word.trim().length() != 0) {
				word= word.toLowerCase(); //其实这是一个bug,word是不可变类,有待....
				break;
			}
		}
		System.out.println("全部转化小写后的单词" + word);
	}
}

2.字符串查找方法

​ (11)boolean contains(CharSequence s): 当且仅当此字符串包含指定的 char 值序列时,返回 true

​ (12)int indexOf(xx):从前往后找当前字符串中xx,即如果有返回第一次出现的下标,要是没有返回-1

​ (13)int lastIndexOf(xx):从后往前找当前字符串中xx,即如果有返回最后一次出现的下标,要是没有返回-1

02.案例应用
import org.junit.Test;
public class Test02 {
		@Test
		public void test1(){
			String str1 = "林大侠是爱编程,编程不仅仅是为了编程";
			System.out.println(str1.indexOf("编程"));//5       返回第一次出现的下标
			System.out.println(str1.lastIndexOf("编程"));//16  有返回最后一次出现的下标
		}
		@Test
		public void test(){
			String str2 = "是你胖了,还是我_(:з」∠)_拿不动了";
			System.out.println(str2.contains("拿不动"));
		}
}

3.字符串–截取方法

​ (14)String substring(int beginIndex) :返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串。

​ (15)String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。

03.案例应用
package com.daxia.case1;
import org.junit.Test;
public class Test03 {
	@Test
	public void test01() {
		String str = "helloworldailindaxia";
		String sub1 = str.substring(5);
		String sub2 = str.substring(5, 10);// substring(a,b)对应区间是[a,b),右边是开区间需要注意!
		System.out.println(sub1);// worldailindaxia
		System.out.println(sub2);// world
	}
	@Test
	public void test02() {
		String fileName = "林大侠爱JAVA,菜鸟一枚.txt";
		// (1)截取文件名
		System.out.println("文件名:" + fileName.substring(0, fileName.lastIndexOf(".")));
		// (2)截取后缀名
		System.out.println("后缀名:" + fileName.substring(fileName.lastIndexOf(".")));

		/*
		 * 打印结果: 文件名:林大侠爱JAVA,菜鸟一枚 后缀名:.txt
		 */
	}
}

4.字符串–字符相关方法

​ (16)char charAt(index):返回[index]位置的字符

​ (17)char[] toCharArray(): 将此字符串转换为一个新的字符数组返回

​ (18)String(char[] value):返回指定数组中表示该字符序列的 String。

​ (19)String(char[] value, int offset, int count):返回指定数组中表示该字符序列的 String。

​ (20)static String copyValueOf(char[] data): 返回指定数组中表示该字符序列的 String

​ (21)static String copyValueOf(char[] data, int offset, int count):返回指定数组中表示该字符序列的 String

​ (22)static String valueOf(char[] data, int offset, int count) : 返回指定数组中表示该字符序列的 String

​ (23)static String valueOf(char[] data) :返回指定数组中表示该字符序列的 String

04.案例应用

​ 校验密码是否合法性。要求密码必须至少11个字符,必须至少3个大写字符,必须包含小写字母和数字,对输入的密码进行比对,符合则返回true,否则返回false。

public class Test04 {
	public static void main(String[] args) {
		String password = "at1DaXiA666";
        //将字符串转成字符数组
		char[] arr = password.toCharArray();
		int upCount = 0; // 大写
		int numCount = 0;// 数字
		int lowerCount = 0;// 小写
       //遍历字符数组,目标就是便于统计字符情况
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] >= 'A' && arr[i] <= 'Z') {
				upCount++;
			} else if (arr[i] >= 'a' && arr[i] <= 'z') {
				lowerCount++;

			} else if (arr[i] >= '0' && arr[i] <= '9') {
				numCount++;
			}
		}
		System.out.print(password + "是否合法(true/false):");
		if (password.length() >= 11 && upCount >= 3 && numCount != 0 && lowerCount != 0) {
			System.out.println(true);
		} else {
			System.out.println(false);
		}
	}
}

5.字符串–开头与结尾方法

​ (24)boolean startsWith(xx):是否以xx开头

​ (25)boolean endsWith(xx):是否以xx结尾

案例应用无

6.字符串–替换方法

​ (26)String replace(xx,xx):不支持正则

​ (27)String replaceFirst(正则,value):替换第一个匹配部分

​ (28)String repalceAll(正则, value):替换所有匹配部分

05.案例应用

​ 替换某字符串中的某字符串。要求键盘录入一个srcStr字符串、录入一个delStr字符串。删除该字srcStr符串中的所有delStr字符串。并且统计delStr字符串在srcStr中出现的次数

import java.util.Scanner;
public class Test05 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入源字符串:");
	    /*public String nextLine()此扫描器执行当前行,并返回跳过的输入信息。
		此方法返回当前行的其余部分,不包括结尾处的行分隔符。当前位置移至下一行的行首。*/
		String srcStr = input.nextLine();
		System.out.print("请输入要删除的字符串:");
		String delStr = input.nextLine();
		// (1)用空字符直接替换目标删除字符,就能得到新的删除后的字符串
		String result = srcStr.replaceAll(delStr, "");
		// (2)(源字符串的长度-删除得到新字符串的长度)/需要删除字符串的长度	
		int count = (srcStr.length() - result.length()) / delStr.length();
		System.out.println(delStr + "一共出现了:" + count + "次");
		System.out.println("删除后的字符串是:" + result);
		input.close();
		/*打印结果:
		请输入源字符串:daxia ai java aijava,hahh
		请输入要删除的字符串:av
		av一共出现了:2次
		删除后的字符串是:daxia ai ja aija,hahh
		*
	}
}

7.字符串–拆分方法

​ (29)String[] split(正则):按照某种规则进行拆分

(无)案例应用

推荐阅读往期博文:

面向对象基础#高频的、必会的-常用类(String、StringBuffer和StringBuilder)归纳总结

#轻松一刻

在这里插入图片描述


☝上述分享来源个人总结,如果分享对您有帮忙,希望您积极转载;如果您有不同的见解,希望您积极留言,让我们一起探讨,您的鼓励将是我前进道路上一份助力,非常感谢!我会不定时更新相关技术动态,同时我也会不断完善自己,提升技术,希望与君同成长同进步!

☞本人博客:https://coding0110lin.blog.csdn.net/  欢迎转载,一起技术交流吧!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值