zoukankan      html  css  js  c++  java
  • equals,toString和String类,StringBuffer类

    1     equals方法

    equals方法,用于比较两个对象是否相同。

    2     toString方法

    toString方法返回该对象的字符串表示,其实该字符串内容就是对象的类型+@+内存地址值。

    3.重写equals方法和toString方法

    复制代码
    public class Person {
        private String name;
        private int age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        //重写equals方法
        public boolean equals(Object obj) {
            if(obj==null){
                return false;
            }
            if(obj==this){
                return true;
            }
            //向下转型
            if(obj instanceof Person){
                Person p=(Person) obj;
                return this.age==p.age;
            }
            return false;
        }
        //重写toString方法
    
        public String toString() {
            
            return "姓名为"+name+"年龄为"+age;
        }
        
    }
    复制代码

    3    String类的概述

    字符串的本质是一个字符的数组。

    复制代码
    public class Demo01 {
        public static void main(String[] args) {
            //""——>String 类对象
            //字符串是常量
            String str="abc";
            String s2=new String("abc");
            System.out.println(str==s2);//false
            System.out.println(str.equals(s2));//true
            
        }
    }
    复制代码

    String方法

    复制代码
    public class Demo02 {
        public static void main(String[] args) {
            //String(byte[]bytes)
            //字节如果为正数走ASCII
            //如果为负数则为汉字,一个汉字为两个字节
            byte[]bytes={
                    49,50,48
            };
            String s1=new String(bytes);
            System.out.println(s1);
            char[]ch={
                    'a','5','e','你'
            };
            String s2=new String(ch,2,2);
            System.out.println(s2);
            //获取字符串长度
            String s3="I love java";
            System.out.println(s3.length());
            //截取字符串,包头不包尾
            String s4="How are you";
            String s44=s4.substring(3);
            System.out.println(s44);
            //判断字符串以谁开头,以谁结尾
            String s5="I am five.java";
            System.out.println(s5.startsWith("I"));
            System.out.println(s5.endsWith(".java"));
            //判断字符串中是否包含另一个字符串
            String s6="oh my god";
            System.out.println(s6.contains("god"));
            //返回该字符串第一次在大字符串中的索引
            System.out.println(s6.indexOf("my"));
            //字符串——>字节数组
            byte[]b=s6.getBytes();
            for(int i=0;i<b.length;i++){
                System.out.println(b[i]);
            }
            //字符串——>字符数组
            char[]c=s6.toCharArray();
            for(int j=0;j<c.length;j++){
                System.out.println(c[j]);
            }
            //忽略大小写比较两个串的内容
            String s7="abc";
            
        }
    }
    复制代码
    复制代码
    public class Demo03 {
        public static void main(String[] args) {
            //判断空字符串
            String s1="";
            System.out.println(s1.isEmpty());
            //获取该字符串中指定位置上的字符
            String s3="change";
            System.out.println(s3.codePointAt(3));
            //转化为小写字符串
            String s4="CHANGE";
            System.out.println(s4.toLowerCase());
            //转化为大写字符串
            String s5="change";
            System.out.println(s5.toUpperCase());
            //在该字符串中,将给定的旧字符,用新字符替换
            String s6="aaa";
            System.out.println(s6.replace("aaa", "bbb"));
            System.out.println(s6.replace('a', 'b'));
            //去除收尾空白
            String s7="  change  ";
            System.out.println(s7.trim());
        }
    }
    复制代码

    练习:题目一:获取指定字符串中,大写字母、小写字母、数字的个数

    复制代码
    public class Demo01 {
        public static void main(String[] args) {
            //    题目一:获取指定字符串中,大写字母、小写字母、数字的个数
            String s1="fdafaDFADF3344";
            int Asum=0;
            int asum=0;
            int nsum=0;
            for(int i=0;i<s1.length();i++){
                char c=s1.charAt(i);
                if('A'<=c&&c<='Z'){
                    Asum+=1;
                }
                else if('a'<=c&&c<='z'){
                    asum+=1;
                }
                else if('0'<=c&&c<='9'){
                    nsum+=1;
                }
            }
            System.out.println("大写字母个数为:"+Asum+"个,小写字母个数为:"+asum+"个,数字个数为:"+nsum+"个。");
        }
    }
    复制代码

    题目二:将字符串中,第一个字母转换成大写,其他字母转换成小写,并打印改变后的字符串。

    复制代码
    public class Demo02 {
        public static void main(String[] args) {
            //    题目二:将字符串中,第一个字母转换成大写,其他字母转换成小写,并打印改变后的字符串。
            String s="ffAAADF";
            String s1=s.substring(0,1);
            String s2=s.substring(1);
            String s3=s1.toUpperCase().concat(s2.toLowerCase());
            System.out.println(s3);
        }
    }
    复制代码

     题目三:查询大字符串中,出现指定小字符串的次数。

    复制代码
    public class Demo03 {
        public static void main(String[] args) {
            //题目三:查询大字符串中,出现指定小字符串的次数。
            //如“hellojava,nihaojava,javazhenbang”中查询出现“java”的次数。
            String s="hellojava,nihaojava,javazhenbang";
            int count=-1;
            int index=0;
            while(index!=-1){
                index=s.indexOf("java");            
                s=s.substring(index+1);
                count++;
            }
            System.out.println(count);
        }
    }
    复制代码

    4    StringBuffer类

    字符串缓冲区支持可变的字符串

    1.    StringBuffer的方法使用:

    复制代码
    public class Demo01 {
        public static void main(String[] args) {
            StringBuffer str=new StringBuffer();
            //链式调用
            str.append("小猪佩奇").append(true).append(1.2);
            System.out.println(str);
            //删除(包头不包尾)
            str.delete(2, 6);
            //插入
            str.insert(2, "熊大");
            System.out.println(str);
            //替换
            str.replace(2, 7, "How are you");
            System.out.println(str);
            //反转
            System.out.println(str.reverse());
        }
    }
  • 相关阅读:
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
  • 原文地址:https://www.cnblogs.com/lxzwhite/p/10694518.html
Copyright © 2011-2022 走看看