JavaApi_String的用法,StringBuffer 的用法

 String类位于java.lang包中,具有丰富的方法计算字符串的长度、比较字符串、连接字符串、提取字符串

String的用法

 1.注册新用户,要求密码长度不能小于6位。

 ----示例

 1 public class Register {
 2         public static void main(String[ ] args) {
 3             Scanner input = new Scanner(System.in);
 4             String uname,pwd;
 5             System.out.print("请输入用户名: ");
 6             uname=input.next();
 7             System.out.print("请输入密码: ");
 8             pwd=input.next();
 9             if( pwd.length()>=6 ){//判断代码长度
10                 System.out.print("注册成功! ");
11             }else{
12                 System.out.print("密码长度不能小于6位!");
13             }        
14     }
15 }

-----计算字符串长度

 1 public int length(){ 2 }  

----调用方法

字符串标识符.length();

2.问题注册成功后,实现登录验证。用户名为“tom”,密码为“1234567”

并分析

String类提供了equals( )方法,比较存储在两个字符串对象的内容是否一致

---示例

public class Login {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            String uname,pwd;        
               System.out.print("请输入用户名: ");
            uname=input.next();
            System.out.print("请输入密码: ");
            pwd=input.next();        
               if( uname.equals("TOM") && pwd.equals("1234567") ){//比较用户名和密码是否正确
                System.out.print("登录成功! ");
            }else{
                System.out.print("用户名或密码不匹配,登录失败!");
            }
        }   
}
 1     // 判断是否是 ""
 2         // System.out.println(s1.isEmpty());
 3 
 4         // 判断是否包含 contains
 5         // System.out.println(s1.contains("ca"));
 6 
 7         // 截取
 8         /**
 9          * System.out.println(s1.substring(2));//ca.mp4
10          * System.out.println(s1.substring(2, 4));//ca
11          **/
12 
13         // 切割 split
14         // System.out.println(Arrays.toString(s2.split(",")));
15 
16         // 大写 小写
17         // s1.toUpperCase();
18         // s1.toLowerCase();
19 
20         // 去除左右的空格
21         // s1.trim();
22 
23         // 替换 replace
24         // System.out.println(s1.replace("a", "f"));//fbcf.mp4
25 
26         // 长度 length
27         /**
28          * if (s1.length() == 0) { System.out.println("请输入密码"); } else if (s1.length() <
29          * 6 || s1.length() > 16) { System.out.println("不好意思 密码必须在6-16之间"); }
30          **/
31 
32         // lastIndexOf 查找出现的位置
33         // System.out.println(s1.lastIndexOf("a"));//3
34 
35         // indexOf 查找出现的位置
36         /**
37          * System.out.println(s1.indexOf("a"));//0
38          * System.out.println(s1.indexOf("a",2));//3
39          * System.out.println(s1.indexOf("x"));//-1
40          **/
41 
42         // 是否是视频格式 endsWith
43         // System.out.println(s1.endsWith(".mp4"));
44 
45         // 取字符串的第一个
46         // System.out.println(s1.charAt(0));//'a'
47 
48         // equals 比较是否相等
49 
50         // 判断谁大
51         // compareTo
52         // compareToIgnoreCase 忽略大小写
53         /**
54          * int result=s1.compareTo(s2); if(result>0) { System.out.println("s1"); }else
55          * if(result==0){ System.out.println("一样大"); }else { System.out.println("s2"); }
56          **/
57 
58         // 判断该字符中存在多少个f
59         String temp = "weursfasdfhasfjweorigdshg" + "dafhsafjsafjafoewruwrqruqw9" + "rhfidsfhasfdcjzvbvsafnakwef"
60                 + "afkhasfhafeiawruwahfasfhads" + "fasfasfasfasfwerwqarhfafafa";
61         
62         /**
63         System.out.println(temp.split("f").length-1);
64         **/
65         
66         /**
67         //替换f为""
68         String newTemp = temp.replace("f", "");
69         System.out.println(temp.length()-newTemp.length());
70         **/
71         
72         /**
73         int count = 0;
74         int idx = temp.indexOf("f");
75         while (idx != -1) {
76             count++;
77             // 从刚刚那个位置上+1然后继续找
78             idx = temp.indexOf("f", idx + 1);
79         }
80         System.out.println(count);
81         **/

StringBuffer 的用法

//为了解决String常见的问题:
        //        String的值无法改变,拼接效率问题
        
        // StringBuffer: 同步,线程安全,速度慢
        // StringBuilder: 不同步,线程不安全,速度快
        // 在这两个类中都有一个方法叫做append追加
        
        StringBuffer buffer=new StringBuffer();
        buffer.append("abc");
        
        //判断一个字符串是否是回文
        //回文字符串是一个正读和反读都一样的字符串
        
        /**
        String str="上海自来水来自海上";
        for (int i = 0; i < str.length()/2; i++) {
            if(str.charAt(i)!=str.charAt(str.length()-1-i)) {
                System.out.println("不是回文");
            }
        }
        **/
        
        /**
        StringBuffer b1=new StringBuffer("上海自来水来自海上");
        //反转 reverse
        StringBuffer b2 = b1.reverse();
        System.out.println(b1.equals(b2));
        **/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值