Java基础--第十三天

讲课流程:

API的使用:

         1,Scanner的应用

                   两次录入:  int -----int :

                               String --- String: 是结束标识符

                               String --- int:

                               int ----- String:不正常[2]   与String --- String有点理解不过来

                                               1,sc重新赋值;

                                               2,两次录入String类型,对第一个进行转换 *

         /*

                   是在String中是结束标识符;但是在录入的开始并没做换行符录入的区别;

                   先输int, 是结束标识符,但是没有纳入int数组里;接下来的String读取开始则从 开始。

         */

         2,String类【构造方法】

                   字符串定义:

                           空串:String s1 = "";//有对象,但是内容为空

                            null:String s2 = null;//没有对象

                   String()

                   String(byte[] bys)

                   String(byte[]bys,int offset,int length)

                   Stringchar[]value)

                   String(byte[]value,int index,int count)

                   String(String str)

         3,String【方法】

                   length();

                            A:字符串一旦被初始化就不能被改变;【实际内容不可以改变,但是引用[地址值]改变了】

                                     注意:这里指的是字符串在常量池里面的值是不能改变的,而不是字符串的引用不能改变    

                            B:String s1 = new String("abcde"); 与 String s2 = "abcde";的区别:

                                     a:在内存中有两个对象存在;

                                     b:在内存中只有一个对象存在。

                   判断方法:

                            boolean equals(Object obj):判断字符串的内容是否相同,区分大小写。

                            boolean equalsIgnoreCase(String str):判断字符串的内容是否相同,不区分大小写。

                            boolean contains(String str):判断字符串对象是否包含给定的字符串。

                            boolean startsWith(String str):判断字符串对象是否以给定的字符串开始。

                            boolean endsWith(String str):判断字符串对象是否以给定的字符串结束。

                            boolean isEmpty():判断字符串对象是否为空。数据是否为空。

                                     String s1 = ""; String s2 = null;

                                     Sop(s1.isEmpty());//true

                                     Sop(s2.isEmpty());//NullPointerException

         4,模拟登录【实例】

                   A:需求

                   B:思路

                   C:实现步骤

 1 package it.cast_01;
 2 
 3 import java.util.Scanner;
 4 
 5 public class UserDemo {
 6     public static void main(String[] args) {
 7         //用户名和密码已有
 8         String username = "admin";
 9         String passward = "admin";
10         Scanner sc = new Scanner(System.in);
11         for (int i = 0; i < 3; i++) {
12             //键盘录入用户名和密码
13             System.out.println("请输入用户名:");
14             String un = sc.nextLine();
15             System.out.println("请输入用户密码:");
16             String pw = sc.nextLine();
17             
18             //判断
19             if(username.equals(un) && passward.equals(pw)){
20                 System.out.println("恭喜您成功登陆!");
21                 //此处可以添加应用代码【猜数字的游戏】
22                 break;
23             }
24             else
25             {
26                 if(i==2)
27                     System.out.println("您的卡已锁,请寻找人工帮助!");
28                 else
29                     System.out.println("登录失败,你还有"+(2-i)+"次机会!");
30             }    
31         }    
32     }
33 }

         5,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)截取字符串。返回从指定位置开始到指定位置结束截取后的字符串。

                                     【包左不包右】

                   对字符串操作完成,原字符串并不更改。

                            实例题:统计字符串中字符个数

                  

 1 package it.cast_03;
 2 
 3 import java.util.Scanner;
 4 
 5 /**
 6  * 统计字符串中各种字符个数
 7  * @author itcast_03
 8  *
 9  */
10 public class CharCount {
11     public static void main(String[] args) {
12         //第一种:
13         //原始字符串
14         String s = "Hello12345World";
15         //统计变量命名及初始化
16         int uperCount = 0;
17         int lowCount = 0;
18         int numCount = 0;
19         //遍历字符串字母
20         for(int i=0;i<s.length();i++)
21         {
22             char ch = s.charAt(i);
23             //每个字母进行判断
24             if(ch>=97 && ch<=122)
25                 lowCount++;
26             else if(ch>=65 && ch<=90)
27                 uperCount++;
28             else if(ch>=48 && ch<=57)
29                 numCount++;
30         }
31         //最终结果输出
32         System.out.println("大写字母有:"+uperCount);
33         System.out.println("小写字母有:"+lowCount);
34         System.out.println("数字字母有:"+numCount);
35         
36         //第二种,优化
37         //键盘录入字符串
38         Scanner sc = new Scanner(System.in);
39         System.out.println("请输入待查询字符串:");
40         String s1 = sc.nextLine();
41         System.out.println(s1);
42         
43         //统计变量命名及初始化
44         uperCount = 0;
45         lowCount = 0;
46         numCount = 0;
47         
48         //遍历字符串字母
49         for(int i=0;i<s1.length();i++)
50         {
51             char ch = s1.charAt(i);
52             //每个字母进行判断
53             if(ch>='a' && ch<='z')
54                 lowCount++;
55             else if(ch>='A' && ch<='Z')
56                 uperCount++;
57             else if(ch>='0' && ch<='9')
58                 numCount++;
59         }
60         //最终结果输出
61         System.out.println("大写字母有:"+uperCount);
62         System.out.println("小写字母有:"+lowCount);
63         System.out.println("数字字母有:"+numCount);
64         
65     }
66 }

                   转换功能:

                            byte[] getBytes():把字符串转换成字节数组。

                            char[] toCharArray():把字符串转换成字符数组。

                            static String copyValueOf(char[] chs):把字符数组转换成字符串。

                            static String valueOf(char[] chs):把字符数组转换成字符串。

                            static String valueOf(int i)基本类型:把int(基本类型)转换成字符串。

                            String toLowerCase():把字符串变成小写

                            String toUpperCase():把字符串变成大写

                           String concat(String str):拼接字符串。

                            链式编程

                            实例:将字符串首字母大写,其余字母小写

 1 package it.cast_05;
 2 /**
 3  * 字符串首字母大写,其余字母小写
 4  * @author Administrator
 5  *
 6  */
 7 public class ChangeTrain {
 8     public static void main(String[] args) {
 9         //原始字符串
10         String s = "HELLoworld";
11         //将首字母截取并变大写
12         String s1 = s.substring(0,1).toUpperCase();
13         //将其余字符提取出来并变小写
14         String s2 = s.substring(1).toLowerCase();
15         //转换完的字符串拼接
16         s = s1.concat(s2);
17         System.out.println(s);
18         //链式编程
19         String s3 = s.substring(0,1).toUpperCase().concat(s.substring(1).toLowerCase());
20         System.out.println(s3);
21     }
22 }

                   其他功能:

                            替换功能

                                     String replace(char oldChar,char newChar):用新的字符去替换指定的旧字符

                                     String replace(String oldString,String newString):用新的字符串去替换指定的旧字符串

                            切割功能

                                    String[] split(String regex)

                            去除字符串两端空格    

                                     String trim()

                            按字典顺序比较两个字符串 

                                     int compareTo(String str)

                            实例:大字符串中截取小字符串

                                     【用少一个字符的字符串替换,然后比较两个字符串长度】

                                     思路:A:定义两个字符串,一个大串,一个小串。

                                          B:在大串中查找第一次出现的索引;没有,这查询完成;出现,则计数器加1;

                                          C:记住上次查询的索引记住,从索引加上小串长度截取字符串;

                                          D:返回B。

                   

原文地址:https://www.cnblogs.com/zhidianhcuan/p/4373461.html