文章目录
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类
- 由多个字符组成的一串数据
- 字符串其本质是一个字符数组
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
:是一个可变的字符串。字符串缓冲区类。
String
和StringBuilder
的区别:
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的相互转换
StringBuilder
转 String
public String toString()
:通过toString()
就可以实现把StringBuilder
转成String
String
转StringBuilder
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);
}
}