String

package myutil;

/**
  *@auther WangZhi
  *@date 2019/8/19 10:20
  */
  
public class TestString {

    public static void main(String[] args) {

        //String类   "abc" 对象   常量区
        String s1 = "abc";
        String s2 = "abc";
        String s3 = new String("abc");
        String s4 = new String("abc");

        System.out.println(s1 == s2);//true
        System.out.println(s1 == s3);//false
        System.out.println(s3 == s4);//false
        System.out.println(s1.equals(s2));//true
        System.out.println(s1.equals(s3));//true
        System.out.println(s3.equals(s4));//true

        /*
          String类如何构建对象
          String str = "abc";//直接将字符串常量赋值给str (字符串常量池)
          String str = new String();//无参构造方法创建控的对象
          String str = new String("abc")//带string参数的构造方法创建对象
          String str = new String(byte);//将数组中的每一个元素转化成对应的char 组合成String
          String str = new String(char[]);//将数组中的每一个char元素拼接成最终的String
         **/


        byte[] value = new byte[]{65,97,48};
        String str = new String(value);
        System.out.println(str);//Aa0

        char[] value1 ={'A','a','0'};
        String str1 = new String(value1);
        String str2 = new String(value1,1,2);
        System.out.println(str1);//Aa0
        System.out.println(str2);//a0



        /*
         String的不可变性
            体现在两个方面, 长度及内容
            String类中包含一个private final 修饰的char[]数组
            长度---> final修饰的数组  数组长度本身不变 final修饰数组的地址不变
            内容---> private修饰的属性 不能在类的外部访问

         String 的常用方法
            1.boolean = equals(Object obj)
                //继承自Object 重写 比较两个字符串中的字面值是否相等
            2.int = hashCode();
                //继承自Object 重写 将当前的字符串的每一个char元素拆开 乘以31求和
            3.int = compareTo(String str);
                //实现自Comparable接口 按照字典(Unicode编码)所索引的顺序比较
            4.String = toString();
                //继承自Object 重写

            5.char = charAt(int index);
            6.int = length();
         **/



        String str = "abcdefg";
        //判断此字符串是否含有a
        boolean value = str.contains("a");
        System.out.println(value);//true
        //判断是否以fg结尾
        boolean value1 = str.endsWith("fg");
        System.out.println(value1);//true
        //判断以ab开头
        boolean value2 = str.startsWith("b");
        System.out.println(value2);//false

        
        //当前字符串转化为数组
        String str = "abcdefg";
        byte[] bytes = str.getBytes();
        for (byte v : bytes) {
            System.out.println((char)v);
            //abcdefg
        }

        char[] chars = str.toCharArray();
        for (char v1 : chars) {
            System.out.println((int)v1);
            /*输出
              97
              98
              99
              100
              101
              102
              103
            */
        }
        
        //字符首次出现的位置
        String str = "abcdefgabc";
        int index = str.indexOf("b");
        System.out.println(index);//1

        //从索引3开始往后找
        String str = "abcdefgabc";
        int index = str.indexOf("b",3);
        System.out.println(index);//8

        //不存在返回-1
        String str = "abcdefgabc";
        int index = str.indexOf("d",5);
        System.out.println(index);//-1

        //lastIndexOf 和 indexOf 差不多


        //字符串是否为空
        String str = "";
        boolean b = str.isEmpty();
        System.out.println(b);//true

        String str = "abc";
        boolean b = str.isEmpty();
        System.out.println(b);//false


        //字符串替换
        //replace();replaceAll();replaceFirst();换第一次出现的
        String str = "abcdef";
        str = str.replace("a","z");
        System.out.println(str);//zbcdef
        
        //字符串拆分
        String str = "abcd";
        String[] value = str.split("");
        for (String v : value) {
            System.out.println(v);
            //a
            //b
            //c
            //d
        }
        //字符串拆分
        String str = "abcd";
        String[] value = str.split("",3);//拆分成3段
        for (String v : value) {
            System.out.println(v);
            //a
            //b
            //cd   
        }

        
        String[] userBox = {"wangzhi-123","wangdong-234","xiexin-321"};
        Scanner input = new Scanner(System.in);
        System.out.println("请输入账号:");
        String name = input.nextLine();
        System.out.println("请输入密码:");
        String password = input.nextLine();

        boolean b = false;
        for(int i = 0;i < userBox.length;i++) {
            //value[0]--name   value[1]--password
            String[] value = userBox[i].split("-");
            if (value[0].equals(name)) {
                if (value[1].equals(password)) {
                    System.out.println("登入成功!");
                    b = true;
                }
                break;
            }
        }
        if(!b) {
            System.out.println("登入失败!用户名或密码错误!");
        }

        /*
        请输入账号:
        wangzhi
        请输入密码:
        123
        登入成功!
        */

        //将当前的字符串截取一部分
        String str = "abcdefgh";
        //str = str.substring(3);//从3号索引一直带最后
        str = str.substring(3,5);//从3号索引刀5号索引
        System.out.println(str);

        String str = "Abcdefgh";
        //str = str.toLowerCase();//字符串全部转成小写
        str = str.toUpperCase();//字符串全部转换成大写
        System.out.println(str);

        //去除字符串前后空格
        String str = "  abcdefgh  ";
        str = str.trim();
        System.out.println(str);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值