String类的基本判断,获取,转换,去空格以及分割

版权声明:此文章最终版权归HrainOne所有,欢迎转载,转载请声明最终版权 https://blog.csdn.net/gaoxin_gx/article/details/90694552

一:判断功能

public class Main{
	public static void main(String[] args) {
		/**
		 * 判断字符串是否相等的几个方法
		 * public boolean equals(Object anObject)	比较两个字符串是否相同
		 * public boolean equalsIgnoreCase(String anotherString)	将此 String 与另一个 String 进行比较,不考虑大小写。
		 * public boolean endsWith(String suffix)	测试此字符串是否以指定的后缀结束。
		 * public boolean startsWith(String prefix)		测试此字符串是否以指定的前缀开始。
		 */
		//创建字符串对象
		String s1 = "hello";
		String s2 = "hello";
		String s3 = "Hello";

		// public boolean equals(Object anObject) 比较两个字符串是否相同
		// 比较s1==s2?
		System.out.println(s1.equals(s2)); // 返回结果:true
		// 比较s1==s3?
		System.out.println(s1.equals(s3)); // 返回结果:false

		// public boolean equalsIgnoreCase(String anotherString) 将此 String 与另一个
		// String 进行比较,不考虑大小写。
		System.out.println(s1.equalsIgnoreCase(s3)); // 返回结果:true

		// public boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束。
		System.out.println(s1.endsWith("llo")); // 返回结果:true
		System.out.println(s1.endsWith("ol")); // 返回结果:false

		// public boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开始。
		System.out.println(s1.startsWith("He")); // 返回结果:false,注意是区分大小写的
		System.out.println(s1.startsWith("he")); // 返回结果:true
	}
}
1)问题练习

输入用户名,密码进行验证,机会只有三次,并有相关的提示语。

import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请设置用户名:");
		String name = sc.nextLine();
		System.out.println("请设置密码:");
		String pass = sc.nextLine();
		System.out.println("你的用户名是:"+name+"\n你的密码是:"+pass+"\n请牢记你的账户和密码");
		System.out.println("请进行登录");
		int count=3;
		String nameIn;
		String passIn;
		while(count>0){
			System.out.println("请输入用户名:");
			nameIn = sc.nextLine();
			System.out.println("请输入密码:");
			passIn = sc.nextLine();
			if(name.equals(nameIn)&&pass.equals(passIn)){
				System.out.println("登录成功!欢迎来到代码的世界!");break;
			}else{
				count = count-1;
				if(count == 0){
					System.out.println("机会用完,账号进行锁定!");break;
				}
				System.out.println("账户或密码错误,请重新输入!,你还有"+count+"次机会");
			}
		}
	}
}

二:获取功能

public class Main{
	public static void main(String[] args) {
		/**
		 * String类的获取功能
		 * int length();获取字符串的长度
		 * char charAt(int index);获取指定索引处的字符
		 * int indexOf(String str);获取str在字符串对象中第一次出现的索引
		 * String substring(int start);从start开始截取字符串
		 * String substring(int start,int end);从start开始,到end结束截取字符串
		 */
		// 创建字符串
		String s1 = "123456";

		// int length();获取字符串的长度
		System.out.println("字符串的长度为" + s1.length());

		// char charAt(int index);获取指定索引处的字符
		System.out.println("索引2处:" + s1.charAt(1)); // 注意索引是从零开始

		// int indexOf(String str);获取str在字符串对象中第一次出现的索引
		System.out.println("2第一次开始的位置:" + s1.indexOf("2")); // 只要涉及到索引,都是从零开始计数

		// String substring(int start);从start开始截取字符串
		System.out.println("从2开始截取:" + s1.substring(1));

		// String substring(int start,int end);从start开始,到end结束截取字符串
		System.out.println("从2开始,5结束" + s1.substring(1, 5)); // 一般情况下,截取都是从左闭右开的情况
	}
}
1)问题练习
遍历字符串
public class Main {
	public static void main(String[] args) {
		// 遍历字符串
		// 创建字符串
		String s1 = "123456789";
		for (int i = 0; i < s1.length(); i++) {
			System.out.println("第" + (i + 1) + "个数字为:" + s1.charAt(i));
		}
	}
}
2)字符串中区分数字,大写字母,小写字母

这解法,相对来说是较为麻烦的,但是主要的目的是了解:
A~Z:65-90
a~z:97-122
数字:48~57

public class Main {
	public static void main(String[] args) {
		String str = "123456asdfgASDFGH";
		int num;
		for (int i = 0; i < str.length(); i++) {
			num = str.charAt(i);
			if (num >= 65 && num <= 90) {
				System.out.println("我是大写:" + (char) num);
			} else if (num >= 97 && num <= 122) {
				System.out.println("我是小写:" + (char) num);
			} else if (num >= 48 && num <= 57) {
				System.out.println("我是数字:" + (char) num);
			}
		}
	}
}

另外一种解法,直接简单明了:

public class Main {
	public static void main(String[] args) {
		String str = "123456asdfgASDFGH";
		char num;
		for (int i = 0; i < str.length(); i++) {
			num = str.charAt(i);
			if (num >= 'A' && num <= 'Z') {
				System.out.println("我是大写:" + num);
			} else if (num >= 'a' && num <= 'z') {
				System.out.println("我是小写:" + num);
			} else if (num >= '0' && num <= '9') {
				System.out.println("我是数字:" + num);
			}
		}
	}
}

三:转换功能

public class Main {
	public static void main(String[] args) {
		/**
		 * String类转换功能 char[] toCharArray();把字符串转换为字符数组 String
		 * toLowerCase();把字符串转换为小写字符串 String toUpperCase();把字符串转换为大写字符串
		 */
		// 创建字符串
		String str = "asdASD";

		// char[] toCharArray();把字符串转换为字符数组
		char[] s1 = str.toCharArray();
		for (int i = 0; i < s1.length; i++) {
			System.out.println("转换为字符数组为:" + s1[i]);
		}

		String str1 = "asdASD";
		// String toLowerCase();把字符串转换为小写字符串
		System.out.println("转换为小写字符串为:" + str1.toLowerCase());

		// String toUpperCase();把字符串转换为大写字符串
		System.out.println("转换为大写字符串为:" + str1.toUpperCase());

	}
}
1)问题练习

一组字符串,要求改进为只有首字母大写,其余小写。

public class Main {
	public static void main(String[] args) {
		// 创建字符串
		String str = "asdASD";
		String str1 = str.toLowerCase();	//全部转换为小写
		char[] str2 = str1.toCharArray();	//转换为数组
		str2[0] = (char) ((int) str2[0] - 32);	//首字母转换为大写
		String str3 = new String(str2);	//将数组转换为字符串
		System.out.println(str3);
	}
}

四:去空格以及分割功能

public class Main {
	public static void main(String[] args) {
		/**
		 * String trim();去除字符串两端的空格 String[] split(String str);按照指定符号分割字符串
		 */
		// 创建字符串
		String str = " a,sd  ";
		// String trim();去除字符串两端的空格
		System.out.println("去除字符串两端的空格:" + str.trim());
		// String[] split(String str);按照指定符号分割字符串
		String[] strarr = str.split("s");
		for (int i = 0; i < strarr.length; i++) {
			System.out.println(strarr[i]);
		}
	}
}
1)问题练习

将字符串进行反转

public class Main {
	public static void main(String[] args) {
		// 创建字符串
		String str = "abc";
		char[] str1 = str.toCharArray();
		char[] str2 = new char[str1.length];
		// 转换为回文
		for (int i = 0; i < str1.length; i++) {
			str2[str1.length - i - 1] = str1[i];
		}
		System.out.println(str2);
	}
}

猜你喜欢

转载自blog.csdn.net/gaoxin_gx/article/details/90694552