String类的入门理解
1、Scanner类的概述和方法
package com.hwadee;
/*
* Scanner类是一个可以使用正则表达式来解析基本类型和字符串的简单文本扫描器
* Scanner使用分隔符模式将其输入分解为标记,默认情况下该分隔符模式与空白匹配。
* 然后可以使用不同的next方法将得到的标记转换为不同类型的值
*
* 目的:接收键盘的输入
* 之前通过的是在开发工具中,手动输入,然后run
*/
import java.util.Scanner;
public class TestScanner {
public static void main(String[] args) {
test1();
test2();
test3();
}
public static void test1() {
Scanner s1=new Scanner(System.in);//system.in叫做标准输入流,用于接收键盘的输入
System.out.println("请您输入密码:");
String nextLine = s1.nextLine();//获取下一个输入型,输出字符串型
System.out.println("请确认您的密码:"+nextLine);
}
public static void test2() {
Scanner s2=new Scanner(System.in);
System.out.println("请输入密码:");
int nextInt = s2.nextInt();//获取下一个输入型,输出int型
System.out.println("您的密码是:"+nextInt);
}
public static void test3() {
Scanner s3=new Scanner(System.in);
System.out.println("请您输入密码:");
while(s3.hasNextLine()) {//hasNextXxx()判断是否还有下一个输入型
String nextLine = s3.nextLine();
System.out.println("请确认您的密码:"+nextLine);
}
}
}
2、String类的构造方法
String类位于java.lang包下,包括计算字符串的长度、比较字符串、连接字符串、提取字符串的功能。
注意查看String类的API文档说明。
package com.hwadee;
//字符串是常量;它们的值在创建之后不能更改
public class TestString {
public static void main(String[] args) {
String name="二狗";
name=name+"小黑";
System.out.println(name);
test1();
test2();
test3();
test4();
}
//将字节数组转换为字符串
public static void test1(){
byte[] b= {97,98,99,100};
String s1=new String(b);
System.out.println(s1);
}
//将字符数组转换为字符串
public static void test2() {
char[] c= {'a','b','c','d','e','f'};
String s2=new String(c);
System.out.println(s2);
}
//在字节数组设置偏移量和长度(length)
public static void test3() {
byte[] be={97,98,99,100,101,102};
String s3=new String(be, 2, 3);
System.out.println(s3);
}
//在字符数组设置偏移量(offset)和数量(count)
public static void test4() {
char[] cr= {'a','b','c','d','e','f'};
String s4=new String(cr, 1, 5);
System.out.println(s4);
}
}
3、String类中的判断功能
package com.hwadee;
/**
* ==号和equals方法的区别:
*
* ==是一个比较运算符号,既可以比较基本数据类型,也可以比较引用数据类型,基本数据类型比较的是值,引用数据类型比较的是两个对象的内存地址。
*
* equals方法是一个方法,只能比较引用数据类型,所有的对象都会继承Object类中的方法
* 通过查看Object类中的源码可知:如果没有重写Object类中的equals方法,equals方法和==号比较引用数据类型无区别。
*
* 小结:默认情况下equals比较的是两个对象的内存地址。
* 但是,这么做是什么多大意义的,所以一般都会重写后的equals方法;在重写的equals方法中比较的两个对象中的属性(成员变量)。
*
*/
public class TestString2 {
public static void main(String[] args) {
String s1 = "hello world";
String s2 = "hello world";
String s3 = "hello" + " world";
String s4 = new String("hello world");
String s5 = new String("hello world");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println("--------------");
System.out.println(s1 == s3);
System.out.println(s1.equals(s3));
System.out.println("--------------");
System.out.println(s1 == s4);//false
System.out.println(s1.equals(s4));//true
System.out.println("---------------");
test1();
test2();
test3();
test4();
test5();
test6();
test7();
}
public static void test1() {
String s1="hello";
String s2="hello";
boolean equals = s1.equals(s2);
System.out.println(equals);
}
public static void test2() {
String s1="Hello";
String s2="hello";
boolean e1 = s1.equals(s2);
System.out.println(e1);
}
public static void test3() {
String s1="Hello";
String s2="hello";
boolean equals = s1.equalsIgnoreCase(s2);
System.out.println(equals);
}
public static void test4() {
String s1="Hello9564";
String s2="lo95";
String s3="lo64";
boolean equals1 = s1.contains(s2);
boolean equals2 = s1.contains(s3);
System.out.println(equals1);
System.out.println(equals2);
}
public static void test5() {
String s1="Hello";
String s2="H";//区分大小写
boolean equals = s1.startsWith(s2);
System.out.println(equals);
}
public static void test6() {
String s1="Hello";
String s2="O";//区分大小写
boolean equals = s1.endsWith(s2);
System.out.println(equals);
}
public static void test7() {
String s1="Hello";
boolean ie = s1.isEmpty();
System.out.println(ie);
}
}
4、String类的获取功能
package com.hwadee;
public class TestString5 {
public static void main(String[] args) {
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
}
public static void test1() {
String s="hello world";
int len = s.length();
System.out.println(len);
}
public static void test2() {
String s="hello world";
char charAt = s.charAt(6);
System.out.println(charAt);
}
public static void test3() {
String s="hello world";
int in = s.indexOf("l");
System.out.println(in);
}
public static void test4() {
String s="hello world";
int in = s.indexOf(101);
System.out.println(in);
}
public static void test5() {
String s="hello world";
int li = s.lastIndexOf("l");
System.out.println(li);
}
public static void test6() {
String s="hello world";
int li = s.lastIndexOf(108);
System.out.println(li);
}
public static void test7() {
String s="hello world";
String substring = s.substring(2);
System.out.println(substring);
}
public static void test8() {
String s="hello world";
String substring = s.substring(2, 7);
System.out.println(substring);
}
}
5、String类的转换功能
package com.hwadee;
public class TestString6 {
public static void main(String[] args) {
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
}
public static void test1() {
String s="abcd";
byte[] bytes = s.getBytes();
for(int i=0;i<bytes.length;i++) {
System.out.println(bytes[i]);
}
}
public static void test2() {
String s="abcd";
char[] charArray = s.toCharArray();
for(int i=0;i<charArray.length;i++) {
System.out.println(charArray[i]);
}
}
public static void test3() {
String s="abcdef";
String upperCase = s.toUpperCase();
System.out.println(upperCase);
}
public static void test4() {
String s="ABCDEFG";
String lowerCase = s.toLowerCase();
System.out.println(lowerCase);
}
public static void test5() {
String s1="abcd";
String s2="qwe";
String s3 = s1.concat(s2);
System.out.println(s3);
}
public static void test6() {
char[] a= {'q','w','e'};
String valueOf = String.valueOf(a);
System.out.println(valueOf);
}
public static void test7() {
String s1=" abcdefghi1234567 ";
System.out.println(s1.length());
String s2 = s1.trim();
System.out.println(s2.length());
System.out.println(s2);
}
public static void test8() {
String s="hallo";
String s2 = s.replace('a', 'e');
System.out.println(s2);
String s3 = s.replace('l', 'v');
System.out.println(s3);
}
public static void test9() {
String s="hello world";
String replacedString = s.replace(" ", "");
System.out.println(replacedString);
}
}
6、String类截取字符串练习
package com.hwadee;
/**
* 利用substring(int beginIndex)获取子串
* 即从开始位置beginIndex至字符串的末尾
*
* public String substring(int beginIndex,int endIndex)
* 获取子串,也通常叫做截取字符串
*
* beginIndex - 起始索引(包括)。
* endIndex - 结束索引(不包括)。
*
* abcdefabc123ab
*
* 请用代码统计子串"ab"在原来的字符串中出现的次数
*/
public class TestString7 {
public static void main(String[] args) {
test1();
test2();
}
public static void test1() {
String s1="abcdefabc123ab";
String s2="ab";
int count=0;
while(s1.indexOf(s2)!=-1) {
count++;
s1=s1.substring(s1.indexOf(s2)+2);
}
System.out.println(count);
}
public static void test2() {
String s1="abcdefabc123ab";
String s2="ab";
int count=0;
for(int i=0;i<=s1.length()-s2.length();i++) {
if(s1.substring(i,i+s2.length()).equals(s2)) {
count++;
}
}
System.out.println(count);
}
}
7、String类的equals方法小练习
package com.hwadee;
import java.util.Scanner;
public class TestString4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String username = "";
String password1 = "";
String password2 = "";
System.out.println("请输入您的用户名:");
username = scanner.nextLine();
System.out.println("请输入您的密码:");
password1 = scanner.nextLine();
System.out.println("请确认密码:");
password2 = scanner.nextLine();
if ("tom".equals(username) && password1.equals(password2)) {
System.out.println("注册成功!");
} else if(password1.equals(password2)) {
System.out.println("用户名错误");
}else if("tom".equals(username)){
System.out.println("密码确认错误!");
System.out.println("请输入您的密码:");
password1 = scanner.nextLine();
System.out.println("请确认密码:");
password2 = scanner.nextLine();
System.out.println("注册成功");
}else {
System.out.println("请重启控制台!!!");
}
}
}
8、StringBuffer类
StringBuffer与String类似,都位于java.lang包下,是String类的增强版。
StringBuffer类是一个方法线程安全的类。String是一个字符串内容(字符序列)不可变的类,而StringBuffer类是一个可变的字符序列。
String转换为StringBuffer:通过构造方法、通过append()方法
StringBuffer转换为String:通过构造方法、通过toString()方法、通过substring(0,length)
StringBuffer与StringBuilder的区别:
StringBuffer是jdk1.0版本的,是线程安全的,效率大部分情况下低于StringBuilder
StringBuilder是jdk1.5版本的,是线程不安全的,效率高
String和StringBuffer,StringBuilder的区别:
String是一个不可变的字符序列
StringBuffer,StringBuilder是可变的字符序列
效率比较:StringBuilder>StringBuffer>String,一般使用StringBuffer
package com.hwadee;
/**
* String
* abc
*
* cba
*
*/
public class TestStringBuffer1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
test5();
}
public static void test1() {
StringBuffer stringBuffer=new StringBuffer("hellohello");
int length = stringBuffer.length();
int capacity = stringBuffer.capacity();
System.out.println("length="+length+",capacity="+capacity);
}
public static void test2() {
StringBuffer stringBuffer=new StringBuffer("hello");
stringBuffer.append(" ");
stringBuffer.append("world");
stringBuffer.append("!");
stringBuffer.append("大家好,我是很帅的郜同学");
String string = stringBuffer.toString();
System.out.println(string);
}
public static void test3() {
StringBuffer stringBuffer=new StringBuffer("hello");
stringBuffer.insert(2, "我是新来的");
String string = stringBuffer.toString();
System.out.println(string);
}
public static void test4() {
StringBuffer stringBuffer=new StringBuffer("hello9527");
stringBuffer.delete(0, 2);
String string = stringBuffer.toString();
System.out.println(string);
}
public static void test5() {
StringBuffer stringBuffer=new StringBuffer("hello9527");
stringBuffer.reverse();
String string = stringBuffer.toString();
System.out.println(string);
}
}