Java笔记day18
接上一次API中的类和方法介绍
一、Scanner
键盘录入工具:Scanner
构造方法:
public Scanner(InputStream source)构造一个新的Scanner ,
产生从指定的输入流扫描的值。
流中的字节将使用底层平台的default charset转换为字符 。
参数
source - 要扫描的输入流
注意:
不能以;类名作为class文件名
查看代码
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
//键盘录入一个int类型的数据
//public int nextInt()将输入的下一个标记扫描为int 。
int num = sc.nextInt();
System.out.println(num);
//键盘录入一个字符串
String s1 = sc.next();
System.out.println(s1);
//键盘录入字符串
public String nextLine()
String s2 = sc.nextLine();
System.out.println("输入的字符串为:" + s2);
//问题:先输入一个整型再输入一个字符串。这里输出输入的数字和字符串
int num = sc.nextInt();
String s3 = sc.next();
System.out.println(num);
System.out.println(s3);
//nextLine()可以读取到特殊的符号,比如换行符,这里会输出输入的数字和一个换行符
//无论nextLine()是在第二行还是第三行结果都是一样
int num = sc.nextInt();
String s4 = sc.nextLine();
System.out.println(num);
System.out.println(s4);
}
}
二、String字符串
1、简单理解:
就是由多个字符组成的数据,叫做字符串
也可以看作是一个字符数组。
2、观察API发现:
1)、String代表的是字符串,属于java.lang下面的,所以使用的时候不需要导包
2)、String类代表字符串。
Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例。(对象)
3)、字符串不变; 它们的值在创建后不能被更改字符串是常量,
一旦被赋值,就不能改变(字符串创建之后,字符串存储在方法去的常量池中,
此处不能改变是指在常量池中不能改变)
3、构造方法:
public String()
public String(byte[] bytes)
public String(byte[] bytes,int offset,int length)
public String(char[] value)
public String(char[] value,int offset,int count)
public String(String original)
4、注意事项:
String类重写了toString()方法
5、对一些方法的解释说明
查看代码
public class StringDemo {
public static void main(String[] args) {
//public String()
String s = new String();
System.out.println("s: " + s);
//查看字符串的长度
//public int length()返回此字符串的长度。
System.out.println("字符串s的长度为:" + s.length());
System.out.println("***************************************");
//public String(byte[] bytes) 将一个字节数组转成一个字符串
byte[] b = {97,98,99,100,101};
String s2 = new String(b);
System.out.println("s2: " + s2);
//查看字符串的长度
//public int length()返回此字符串的长度。
System.out.println("字符串s的长度为:" + s2.length());
System.out.println("***************************************");
//public String(byte[] bytes,int index,int length)
//将字节数组中的一部分截取出来变成一个字符串
String s3 = new String(b, 1, 3);
System.out.println("s3: " + s3);
//查看字符串的长度
//public int length()返回此字符串的长度。
System.out.println("字符串s的长度为:" + s3.length());
System.out.println("***************************************");
//StringIndexOutOfBoundsException
//String s4 = new String(b, 1, 5);
//System.out.println("s4: " + s4);
//查看字符串的长度
//public int length()返回此字符串的长度。
//System.out.println("字符串s的长度为:" + s4.length());
System.out.println("***************************************");
//public String(char[] value) 将一个字符数组转成一个字符串
char[] c = {'a','b','c','d','我','爱','冯','提','莫'};
String s5 = new String(c);
System.out.println("s5: " + s5);
//查看字符串的长度
//public int length()返回此字符串的长度。
System.out.println("字符串s的长度为:" + s5.length());
System.out.println("***************************************");
//public String(char[] value,int offset,int count)
//将字符数组中一部分截取出来变成一个字符串
String s6 = new String(c, 4, 5);
System.out.println("s6: " + s6);
//查看字符串的长度
//public int length()返回此字符串的长度。
System.out.println("字符串s的长度为:" + s6.length());
System.out.println("***************************************");
//public String(String original)
String s7 = "你好";
String s8 = new String(s7);
System.out.println("s8: " + s8);
//查看字符串的长度
//public int length()返回此字符串的长度。
System.out.println("字符串s的长度为:" + s8.length());
}
}
6、String s1 = new String(“hello”)和String s2 = “hello”的区别?
String s1 = new String(“hello”);
此处是通过栈中的String s指向堆内存空间的new String()的地址值,而此处堆内存空间又指向方法区中的常量池地址值
String s2 = “hello”
此处的String s2是直接指向方法区中的常量池的地址值
1)、==比较引用数据类型的时候,比较的是地址值
2)、String s1 = new String("hello");会在堆内存中创建对象
3)、String类中重写了Object的equals方法
4)、equals方法默认比较的是地址值,但是由于重写了,所以比较的是内容
查看代码
public class StringDemo3 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = "hello";
System.out.println(s1==s2); //false,比较的是s1和s2的地址值
System.out.println(s1.equals(s2)); //true,equals被重写了,这里比较的是s1和s2的字符串内容
}
}
5)、例题
/*
看程序写结果
*/
查看代码
public class StringDemo4 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1==s2);
//false,s1,s2指向的堆内存空间地址值不同,但是堆内存空间指向的常量池地址值相同
System.out.println(s1.equals(s2));
//true
String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3==s4);
//false,s3指向堆内存空间,s4指向常量池
System.out.println(s3.equals(s4));
//true
String s5 = "hello";
String s6 = "hello";
System.out.println(s5==s6);
//true
System.out.println(s5==s4);
//true,s4,s5,s6指向同一处常量池
System.out.println(s5.equals(s6));
//true
}
}
7、字符串相加
1)、字符串如果是变量相加,是先开辟空间,然后再拼接
2)、字符串如果是常量相加,是先相加,然后去常量池中找,如果找到了,就返回,如果找不到就创建一个新的
查看代码
public class StringDemo5 {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
String s4 = "hello"+"world";
String s5 = s1+s2;
System.out.println(s3==s4);
//true,s3指向常量池中的"helloworld",s4常量相加之后在常量池中寻找,找到"helloworld"
//之后再指向它,所以s3,s4指向的是同一个常量池
System.out.println(s3==s1+s2);
//false,s1+s2是变量相加,先在常量池中开辟一个空间,再拼接,而且此时它表示的是拼接的helloworld,
//并不是helloworld字符串,所以和s3指向的不是同一个常量池
System.out.println(s3.equals(s1+s2));
//true,字符串相同,所以是true
//System下面一个的方法可以看地址值
//public static int identityHashCode(Object x)
System.out.println(System.identityHashCode(s3));//1163157884
System.out.println(System.identityHashCode(s4));//1163157884
System.out.println(System.identityHashCode(s5));//1956725890
}
}
8、String类的判断功能
boolean equals(Object obj)
boolean equalsIgnoreCase(String str)
boolean contains(String str)
boolean startsWith(String str)
boolean endsWith(String str)
boolean isEmpty()
查看代码
public class StringDemo6 {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "Helloworld";
String s3 = "HelloWorld";
//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 contains(String str)
//当且仅当此字符串包含指定的char值序列时才返回true。
//判断大的字符串中是否包含小的字符串,如果包含,返回true,反之false
//区分大小写,譬如下面的一个的意思是判断s1中包不包含Hello
System.out.println(s1.contains("Hello"));
System.out.println(s1.contains("leo"));
System.out.println(s1.contains("hello"));
System.out.println("**************************");
//boolean startsWith(String str)
//测试此字符串是否以指定的前缀开头。
//区分大小写
System.out.println(s1.startsWith("hel"));
System.out.println(s1.startsWith("h"));
System.out.println(s1.startsWith("he"));
System.out.println(s1.startsWith("he34"));
System.out.println(s1.startsWith("H"));
System.out.println("**************************");
//boolean endsWith(String str)
//测试此字符串是否以指定的后缀结束。
//区分大小写
System.out.println(s1.endsWith("orld"));
System.out.println(s1.endsWith("orlD"));
System.out.println("**************************");
//boolean isEmpty() 判断字符串是否是空字符串
//是返回true,不是返回false
System.out.println(s1.isEmpty());
System.out.println("**************************");
String s4 = "";
String s5 = null;
System.out.println(s4==s5);
System.out.println(s4.isEmpty());
//NullPointerException,这里的s5并没有分配地址值,不能调用方法
//System.out.println(s5.isEmpty());
System.out.println("**************************");
String s6 = "bigdata";
String s7 = null;
//System.out.println(s6.equals(s7));
//System.out.println(s7.equals(s6));
/**
* 字符串之间比较的要求,在不知道两个字符串变量的值的时候,为了防止
* 空指针异常,把变量放在后面
*/
//需求:将s6,s7与"hadoop"进行比较
//所以推荐下面的第二种写法
//第一种
//System.out.println(s6.equals("hadoop"));
//System.out.println(s7.equals("hadoop"));
/*
第二种(推荐写法)
*/
System.out.println("hadoop".equals(s6));
System.out.println("hadoop".equals(s7));
}
}
9、String类的获取功能
*int length()
*char charAt(int index)
int indexOf(int ch)
int indexOf(String str)
int indexOf(int ch,int fromIndex)
int indexOf(String str,int fromIndex)
*String substring(int start)
*String substring(int start,int end)
查看代码
public class StringDemo7 {
public static void main(String[] args) {
String s = "helloworld";
//int length() 获取字符串的长度
System.out.println(s.length());
System.out.println("*************************");
//char charAt(int index) 返回char字符指定位置索引的值
//索引从0开始到length()-1
System.out.println(s.charAt(7));
System.out.println(s.charAt(0));
//StringIndexOutOfBoundsException
// System.out.println(s.charAt(100));
System.out.println("*************************");
//int indexOf(int ch)
//返回指定字符第一次出现的字符串内的索引
//如果此字符串中没有此类字符,则返回-1 。
System.out.println(s.indexOf('l')); //这里是L不是1
System.out.println(s.indexOf(97)); //a,-1
System.out.println("*************************");
//int indexOf(String str)
//helloworld
//返回的是字符串第一个字符在大字符串中的索引值
//如果k的值不存在,则返回-1
System.out.println(s.indexOf("owo")); //4
System.out.println(s.indexOf("qwer")); //-1
System.out.println("*************************");
//int indexOf(int ch,int fromIndex)
//返回指定字符第一次出现在字符串中的索引,以指定的索引开始搜索
System.out.println(s.indexOf('l',4));//8
System.out.println(s.indexOf('p',4)); //-1
System.out.println(s.indexOf('l',40));//-1
System.out.println(s.indexOf('p',40));//-1
System.out.println("*************************");
//int indexOf(String str,int fromIndex)
//以指定的索引开始搜索字符串,返回字符串第一次出现的位置
System.out.println("*************************");
//String substring(int start)
//返回的是一个字符串,该字符串是此字符串的子字符串
//子字符串从指定的索引处开始,并扩展到字符拆的末尾
//包含开始索引位置的值
//helloworld
System.out.println(s.substring(3)); //loworld
//.StringIndexOutOfBoundsException
//System.out.println(s.substring(20));
System.out.println("*************************");
//String substring(int start,int end)
//返回的是一个字符串,该字符串是此字符串的子字符串
//字串开始于start位置,并截取到end-1的位置
//左闭右开 [,) 含头不含尾
System.out.println(s.substring(5,10));//world
//StringIndexOutOfBoundsException
//System.out.println(s.substring(1,20));
}
}
10、String的转换功能
byte[] getBytes()
char[] toCharArray()
static String valueOf(char[] chs)
static String valueOf(int i)
String toLowerCase()
String toUpperCase()
String concat(String str)
查看代码
public class StringDemo8 {
public static void main(String[] args) {
String s = "HelloWorLD";
//byte[] getBytes()
//使用平台的默认字符集将此String编码为字节序列,将结果存储到新的字节数组中。
//此处
byte[] b1 = s.getBytes();
//System.out.println(b1); //[B@4554617c
for(int i=0;i<b1.length;i++){
System.out.println(b1[i]);
}
System.out.println("***********************************");
//char[] toCharArray()
//将字符串转换成字符数组
//字符串 --> 字符数组
char[] c1 = s.toCharArray();
for(int i=0;i<c1.length;i++){
System.out.print(c1[i]);
}
//增强for循环,后面集合的时候会讲解,它是用来替代迭代器的。
//for(char c : c1){
// System.out.print(c);
// }
System.out.println();
System.out.println("***********************************");
//static String valueOf(char[] chs)
//将字符数组转换成字符串
String s1 = String.valueOf(c1);
System.out.println(s1);
System.out.println("***********************************");
//static String valueOf(int i)
//将int类型的数据转换成字符串类型
String s2 = String.valueOf(100); //100 -->"100",将int型的100转为String类型的100
System.out.println(s2); //100
System.out.println("***********************************");
//String toLowerCase()
//将字符串中的内容全部转换成小写
String s3 = s.toLowerCase();
System.out.println(s3); //helloworld
System.out.println("***********************************");
//String toUpperCase()
String s4 = s.toUpperCase();
System.out.println(s4); //HELLOWORLD
System.out.println("***********************************");
//String concat(String str)
//将小括号中的字符串拼接到大字符串的后面
String s5 = s.concat("hadoop");
System.out.println(s5);
}
}
11、String的替换功能
替换功能
String replace(char old,char new)
String replace(String old,String new)
去除字符串两空格
String trim()
按字典顺序比较两个字符串
int compareTo(String str)
int compareToIgnoreCase(String str)
查看代码
public class StringDemo11 {
public static void main(String[] args) {
String s = "helloworldowodadadwowo";
//String replace(char old,char new)
//将新的字符替换字符中指定的所有字符,并返回新的字符串
String s1 = s.replace('l', 'a'); //是L,不是1
System.out.println(s1);
System.out.println(s);
System.out.println("******************************");
//String replace(String old,String new)
//将字符串中旧的小串用新的小串替换,返回一个新的字符串
String s2 = s.replace("owo", "wow");
System.out.println(s2);
String s3 = s.replace("owo", "qwerdf");
System.out.println(s3);
//如果被替换的字符串不存在,返回的是原本的字符串
String s4 = s.replace("qwer", "poiu");
System.out.println(s4);
System.out.println("***********************************");
//String trim() 去除字符串两边的空格
String s5 = " hello world ";
System.out.println(s5);
System.out.println(s5.trim()); //"hello world"
System.out.println("***********************************");
//int compareTo(String str)
String s6 = "hello"; //h的ASCII码值104
String s7 = "hello";
String s8 = "abc"; //a的ASCII码值97
String s9 = "qwe"; //q的ASCII码值113
System.out.println(s6.compareTo(s7)); //0 s6-s7
System.out.println(s6.compareTo(s8)); //7 s6-s8
System.out.println(s6.compareTo(s9)); //-9 s6-s9
String s10 = "hel";
System.out.println(s6.compareTo(s10)); //2
//int compareTo(String str)的意思是,如果两个字符串从开始就不一样,
//那就直接用前面一个数组的ASCLL码值减去第二个,如果是一样的就一直往后延,
//直到不一样时再减,如果一个字符串被另一个包含,就直接用前一个字符串的长度
//减去第二个字符串的长度
}
}
12、字符串应用例题
1)、遍历获取字符串中的每一个字符
统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
查看代码
public class StringDemo9 {
public static void main(String[] args) {
String s = "hadoopjavaMySQL12138";
//遍历获取字符串中的每一个字符
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i]);
}
System.out.println();
//统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
//定义三个变量统计三个结果
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
//遍历字符数组,得到每一个字符,直接比较的是字符的ASCLL码
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (c >= 'A' && c <= 'Z') {
bigCount++;
} else if (c >= 'a' && c <= 'z') {
smallCount++;
} else if (c >= '0' && c <= '9') {
numberCount++;
}
}
//输出结果
System.out.println("大写字符的个数为:" + bigCount);
System.out.println("小写字符的个数为:" + smallCount);
System.out.println("数字字符的个数为:" + numberCount);
}
}
2)、需求:将一个字符串的首字母转成大写,其余字母转成小写
举例:"hADoopJava" ---> "Hadoopjava"
分析:
1、全部转成小写
2、获取第一个字符,转成大写
查看代码
public class StringDemo10 {
public static void main(String[] args) {
String s = "hADoopJava";
//将字符串中每一个字符转小写
String s1 = s.toLowerCase();
//获取第一个字符
String s2 = s1.substring(0, 1);
//将第一个字符转大写
String s3 = s2.toUpperCase();
//获取除第一个字符以外的字符串
String s4 = s1.substring(1);
//将转大写的字符与后面所有小写的进行拼接
String res = s3.concat(s4);
System.out.println(res);
//s.toLowerCase().substring(0, 1).toUpperCase.
//concat(s.toLowerCase().substring(1)),下面的是优化
System.out.println("****链式编程*********************");
String res2 = s.substring(0, 1)
.toUpperCase()
.concat(s.substring(1).toLowerCase());
System.out.println(res2);
}
}
3)、字符串反转
举例:键盘录入”abc” 输出结果;”cba”
分析;
1、导包并创建键盘录入对象
2、创建一个空的字符串
3、将字符串转换成字符数组
4、倒着遍历,得到每一个字符
4、将每次获取到的字符拼接
5、输出
查看代码
import java.util.Scanner;
public class StringDemo12 {
public static void main(String[] args) {
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
//键盘输入一个字符串
String s = sc.next();
//定义一个空的字符串
String s1 = "";
//将输入的字符串转换成字符数组
char[] chars = s.toCharArray();
//倒着遍历,得到每一个字符
for (int i = chars.length - 1; i >= 0; i--) {
//将每次获取到的字符拼接
s1 += chars[i];
}
System.out.println("倒序后的字符串为:" + s1);
}
}
4)、统计大串中小串出现的次数
举例:在字符串” woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun”
中java出现了5次
第一种
查看代码
public class Test2 {
public static void main(String[] args) {
String s = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
char[] chars = s.toCharArray();
int sum = 0;
for (int i = 0; i < chars.length; i++) {
if (((chars[i]) == 106) && ((chars[i + 1]) == 97) &&
((chars[i + 2]) == 118) && ((chars[i + 3]) == 97)) {
sum++;
}
}
System.out.println(sum);
}
}
第二种
查看代码
public class CountTset2 {
public static void main(String[] args) {
String s = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
String minString = "java";
int count = 0;
int index = s.indexOf(minString);
if (index == -1) {
System.out.println("该大串中没有小串");
} else {
while (index != -1) {
count++;
int startIndex = index + minString.length();
s = s.substring(startIndex);
index = s.indexOf(minString);
}
}
System.out.println("Java出现的次数为:" + count);
}
}