java常用API Scanner、String、StringBuilder

1. Scanner类接受键盘录入的字符串

用Scanner类的方法可以完成接收键盘录入的数据

import java.util.Scanner;

/*
 * Scanner:用于获取键盘录入的数据。(基本数据类型,字符串数据)
 * 		public String nextLine():获取键盘录入的字符串数据
 */
public class ScannerDemo {
	public static void main(String[] args) {
		//创建键盘录入对象
		Scanner sc = new Scanner(System.in);
		
		//接收数据
		System.out.println("请输入一个字符串数据:");
		String s = sc.nextLine();
		
		//输出结果
		System.out.println("s:"+s);
	}
}

2. String类

  1. 由多个字符组成的一串数据
  2. 字符串其本质是一个字符数组

2.1 常用构造方法:

  • String(String original):把字符串数据封装成字符串对象
  • (String(char[] value):把字符数组的数据封装成字符串对象
  • String(char[] value, int index, int count):把字符数组中的一部分数据封装成字符串对象
public class StringDemo {
	public static void main(String[] args) {
		//方式1
		//String(String original):把字符串数据封装成字符串对象
		String s1 = new String("hello");
		System.out.println("s1:"+s1);
		System.out.println("---------");
		
		//方式2
		//String(char[] value):把字符数组的数据封装成字符串对象
		char[] chs = {'h','e','l','l','o'};
		String s2 = new String(chs);
		System.out.println("s2:"+s2);
		System.out.println("---------");
		
		//方式3
		//String(char[] value, int index, int count):把字符数组中的一部分数据封装成字符串对象
		//String s3 = new String(chs,0,chs.length);
		String s3 = new String(chs,1,3);
		System.out.println("s3:"+s3);
		System.out.println("---------");
		
		//方式4
		String s4 = "hello";
		System.out.println("s4:"+s4);
	}
}

在这里插入图片描述
通过构造方法创建的字符串对象和直接赋值方式创建的字符串对象有什么区别呢?

  • 通过构造方法创建字符串对象是在堆内存
  • 直接赋值方式创建对象是在方法区的常量池

"=="符号:

  • 基本数据类型:比较的是基本数据类型的值是否相同
  • 引用数据类型:比较的是引用数据类型的地址值是否相同
public class StringDemo2 {
	public static void main(String[] args) {
		String s1 = new String("hello");
		String s2 = "hello";
		
		System.out.println("s1:"+s1);
		System.out.println("s2:"+s2);
		
		System.out.println("s1==s2:"+(s1==s2)); //false
		
		String s3 = "hello";
		System.out.println("s1==s3:"+(s1==s3)); //false
		System.out.println("s2==s3:"+(s2==s3)); //true
	}
}

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

2.2 String类的判断功能

  • boolean equals(Object obj):比较字符串的内容是否相同
  • boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
  • boolean startsWith(String str):判断字符串对象是否以指定的str开头
  • boolean endsWith(String str):判断字符串对象是否以指定的str结尾
public class StringDemo {
	public static void main(String[] args) {
		//创建字符串对象
		String s1 = "hello";
		String s2 = "hello";
		String s3 = "Hello";
		
		//boolean equals(Object obj):比较字符串的内容是否相同
		System.out.println(s1.equals(s2));
		System.out.println(s1.equals(s3));
		System.out.println("-----------");
		
		//boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
		System.out.println(s1.equalsIgnoreCase(s2));
		System.out.println(s1.equalsIgnoreCase(s3));
		System.out.println("-----------");
		
		//boolean startsWith(String str):判断字符串对象是否以指定的str开头
		System.out.println(s1.startsWith("he"));
		System.out.println(s1.startsWith("ll"));
		System.out.println("-----------");
		
		//boolean endsWith(String str):判断字符串对象是否以指定的str结尾
		System.out.println(s1.endsWith("lo"));
		System.out.println(s1.endsWith("oo"));
		System.out.println("-----------");
	}
}

在这里插入图片描述

2.3 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结束截取字符串。包括start,不包括end
public class StringDemo {
	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(0));
		System.out.println(s.charAt(1));
		System.out.println("--------");
		
		//int indexOf(String str):获取str在字符串对象中第一次出现的索引
		System.out.println(s.indexOf("l"));
		System.out.println(s.indexOf("owo"));
		System.out.println(s.indexOf("ak"));
		System.out.println("--------");
		
		//String substring(int start):从start开始截取字符串
		System.out.println(s.substring(0));
		System.out.println(s.substring(5));
		System.out.println("--------");
		
		//String substring(int start,int end):从start开始,到end结束截取字符串
		System.out.println(s.substring(0, s.length()));
		System.out.println(s.substring(3,8));
	}
}

在这里插入图片描述

2.4 String类的转换功能

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

在这里插入图片描述

2.5 String 去除空格,按照指定符号分割字符串

  • String trim():去除字符串两端空格
  • String[] split(String str):按照指定符号分割字符串
public class StringDemo {
	public static void main(String[] args) {
		//创建字符串对象
		String s1 = "helloworld";
		String s2 = "  helloworld  ";
		String s3 = "  hello  world  ";
		System.out.println("---"+s1+"---");
		System.out.println("---"+s1.trim()+"---");
		System.out.println("---"+s2+"---");
		System.out.println("---"+s2.trim()+"---");
		System.out.println("---"+s3+"---");
		System.out.println("---"+s3.trim()+"---");
		System.out.println("-------------------");
		
		//String[] split(String str)
		//创建字符串对象
		String s4 = "aa,bb,cc";
		String[] strArray = s4.split(",");
		for(int x=0; x<strArray.length; x++) {
			System.out.println(strArray[x]);
		}
	}
}

在这里插入图片描述

3. StringBuilder类

3.1 StringBuilder 构造和成员方法

StringBuilder:是一个可变的字符串。字符串缓冲区类。

StringStringBuilder的区别:

  • String的内容是固定的
  • StringBuilder的内容是可变的

+=拼接字符串耗费内存原因:
在这里插入图片描述
每次拼接都会产生新的字符串对象,而利用StringBuilder来拼接字符串自始至终用的都是同一个StringBuilder容器。

  • A:构造方法:
    • StringBuilder()
  • B:成员方法:
    • public int capacity():返回当前容量 (理论值)
    • public int length():返回长度(已经存储的字符个数)
    • public StringBuilder append(任意类型):添加数据,并返回自身对象
    • public StringBuilder reverse():反转功能
public class StringBuilderDemo {
	public static void main(String[] args) {
		//创建对象
		StringBuilder sb = new StringBuilder();
		System.out.println("sb:"+sb);
		System.out.println("sb.capacity():"+sb.capacity());
		System.out.println("sb.length():"+sb.length());
	}
}

在这里插入图片描述

public class StringBuilderDemo {
	public static void main(String[] args) {
		//创建对象
		StringBuilder sb = new StringBuilder();
		
		//public StringBuilder append(任意类型)
		StringBuilder sb2 = sb.append("hello");
		
		System.out.println("sb:"+sb);
		System.out.println("sb2:"+sb2);
		System.out.println(sb == sb2); //true

		sb.append("hello");
		sb.append("world");
		sb.append(true);
		sb.append(100);

		//链式编程
		sb.append("hello").append("world").append(true).append(100);
		
		System.out.println("sb:"+sb);
		
		//public StringBuilder reverse()
		sb.reverse();
		System.out.println("sb:"+sb);
	}
}

在这里插入图片描述

3.2 StringBuilder和String的相互转换

StringBuilderString

  • public String toString():通过toString()就可以实现把StringBuilder转成String

StringStringBuilder

  • StringBuilder(String str):通过构造方法就可以实现把String转成StringBuilder
public class StringBuilderTest {
	public static void main(String[] args) {
		
		//StringBuilder转String
		StringBuilder sb = new StringBuilder();
		sb.append("hello").append("world");
		String s = sb.toString();
		System.out.println(s);

		//String转StringBuilder
		String s2 = "helloworld";
		StringBuilder sb2 = new StringBuilder(s2);
		System.out.println(sb2);
	}
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超级D洋葱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值