Java新手:String类的一些功能方法

String 类:是字符串类,有多个字符组成的一串数据。

1.string类是java.lang包下的,所以使用时不需要导入包。字符串是常量,一旦被赋值,就不能被改变,字符串本质上是一个字符数组。字符串字面值“abc”也可以看成是一个字符串对象。
2.String类的构造方法:

public static void main(String[] args) {
		//方法一:String(String original);把字符串数据封装成字符串对象
		String s1 = new String("hello");
		System.out.println("s1:"+s1);
		System.out.println("---------------");
		//方法二:String(char[] value);把字符数组的数据封装成字符串对象
		char[] chs ={'a','b','c','d'};
		String s2=new String(chs);
		System.out.println("s2:"+s2);
		System.out.println("---------------");
		//方法三:String(char[] value,int index,int count)把字符数组中的一部分数据封装成字符串对象,从index开始的这count个字符拿出
		String s3 = new String(chs,1,2);
		System.out.println("s3:"+s3);
		System.out.println("---------------");
		//方法四
		String s4="world";
		System.out.println("s4:"+s4);
	}

输出结果:

s1:hello
---------------
s2:abcd
---------------
s3:bc
---------------
s4:world

3.String类的判断功能:

public static void main(String[] args) {
		//判断
		String s1 = new String("hello");
		String s2="hello";
		String s3="hello";
		//==:如果是基本数据类型,比较的是值是否相同,如果是引用数据类型,比较的是地址是否相同
		System.out.println("s1==s2"+s1==s2);
		//比较字符串的内容是否相同
		System.out.println("s1.equals(s2)"+s1.equals(s2));
		System.out.println("s2==s3"+s2==s3);
		System.out.println("s2.equals(s3)"+s2.equals(s3));
		String s4="Hello";
		System.out.println("s3.equals(s4)"+s3.equals(s4));
		//比较字符串的内容是否相同,忽略大小写
		System.out.println("s3.equalsIgnoreCase(s4)"+s3.equalsIgnoreCase(s4));
		//判断字符串对象是否以指定的字符串开头
		System.out.println("s2.startsWith('he')"+s2.startsWith("he"));
		System.out.println("s2.startsWith('ll')"+s2.startsWith("ll"));
		//判断字符串对象是否以指定的字符串结尾
		System.out.println("s2.endsWith('he')"+s2.endsWith("he"));
		System.out.println("s2.endsWith('lo')"+s2.endsWith("lo"));
	}

输出结果:

false
s1.equals(s2)true
false
s2.equals(s3)true
s3.equals(s4)false
s3.equalsIgnoreCase(s4)true
s2.startsWith('he')true
s2.startsWith('ll')false
s2.endsWith('he')false
s2.endsWith('lo')true

注意:通过构造方法创建字符串对象是在堆内存中,直接赋值方法是在方法区的常量池中。

4.判断的例子

需求:

模拟登录,给三次机会,并提示还有几次机会

功能实现:

首先,定义两个字符串对象,用于存储已经存在的用户名和密码

public static void main(String[] args) {
		String username="admin";
		String password = "123456";
		for(int i=0;i<3;i++){
		//使用Scanner键盘录入
			Scanner sc =new Scanner(System.in);
			System.out.println("请输入用户名");
			//这是键盘录入字符串的代码。
			String uname = sc.nextLine();
			System.out.println("请输入密码");
			String pwd = sc.nextLine();
			//用于字符串的比较,比较字符串的内容是否相同
			if(uname.equals(username) && pwd.equals(password)){
				System.out.println("登录成功");
               //要登录成功,退出for循环
				break;
			}else{
		      //i是从0开始的,所以2-i
				if((2-i)==0){
					System.out.println("已经连续失败3次,用户名和密码被锁定");
				}else{
					System.out.println("登录失败,还有"+(2-i)+"次机会");
				}
			}
		}
	}

测试例子结果:

请输入用户名
admin
请输入密码
123
登录失败,还有2次机会
请输入用户名
admin1
请输入密码
123456
登录失败,还有1次机会
请输入用户名
admin
请输入密码
123456
登录成功

特意说明:只是Java基础中的小例子,没有任何框架在里面。

5.String 类获取的功能

public static void main(String[] args) {
		//获取
		String s="helloworld";
		//int length()截取字符串的长度,即是字符个数
		System.out.println("字符串的长度:"+s.length());
		System.out.println("-------------");
		//char charAt(int index)获取索引处的字符
		System.out.println("第二个字符是"+s.charAt(1));
		System.out.println("-------------");
		//int indexOf(String str)获取str在字符串对象中第一次出现的索引,以第一个字符为准的索引,字符串中不包含的返回-1.
		System.out.println("llo第一次出现的索引是"+s.indexOf("llo"));
		System.out.println("owo第一次出现的索引是"+s.indexOf("owoo"));
		System.out.println("-------------");
		//String subString(int start)从start开始截取字符串
		System.out.println("截取w以后的字符:"+s.substring(5));
		//String subString(int start,int end)从start开始,到end结束截取字符串,包括start,不包括end.
		System.out.println("截取hello字符:"+s.substring(0,5));
	}

输出结果:

字符串的长度:10
-------------
第二个字符是e
-------------
llo第一次出现的索引是2
owo第一次出现的索引是-1
-------------
截取w以后的字符:world
截取hello字符:hello

6.字符串的转换

public static void main(String[] args) {
		//转换
		String s="HelloWorld";
		//char[] toCharArray():把字符串转换为字符数组
		char[] chs = s.toCharArray();
		for(int i=0;i<chs.length;i++){
			System.out.print(chs[i]+"/");
		}
		System.out.println();
		System.out.println("------------");
		//String toLowerCase():把字符串转换为小写字符串
		System.out.println(s.toLowerCase());
		//String toUpperCase():把字符串转换为大写字符串
		System.out.println(s.toUpperCase());
	}

输出结果:

H/e/l/l/o/W/o/r/l/d/
------------
helloworld
HELLOWORLD

7.遍历字符串

public static void main(String[] args) {
		//遍历字符串
		//方法一
		String s="helloworld";
		for(int i=0;i<s.length();i++){
			System.out.print(s.charAt(i));
		}
		//方法二
		char[] chs = s.toCharArray();
		for(int i=0;i<chs.length;i++){
			System.out.print(chs[i]);
		}
	}

输出结果:

helloworld

8.去除字符串两端空格

public static void main(String[] args) {
		//String trim()
		String s="helloworld";
		String s2="  helloworld   ";
		String s3="  hello world   ";
		System.out.println("s2:"+s2);
		System.out.println("s2.trim():"+s2.trim());
		System.out.println("-------------");
		System.out.println("s3:"+s3);
		System.out.println("s3.trim():"+s3.trim());
	}

输出结果:

s2:  helloworld   
s2.trim():helloworld
-------------
s3:  hello world   
s3.trim():hello world

9.按照指定符号分割字符串

public static void main(String[] args) {
		//String[] split(String str)
		String s="a/b/c/d/e";
		String[] chs=s.split("/");
		for(int i=0;i<chs.length;i++){
			System.out.println(chs[i]);
		}
	}

输出结果:

a
b
c
d
e

猜你喜欢

转载自blog.csdn.net/w_e_i_1201/article/details/83045744