• Java自学-数字与字符串 比较字符串


    Java 比较字符串

    示例 1 : 是否是同一个对象

    str1和str2的内容一定是一样的!
    但是,并不是同一个字符串对象

    package character;
     
    public class TestString {
     
        public static void main(String[] args) {
     
            String str1 = "the light";
             
            String str2 = new String(str1);
             
            //==用于判断是否是同一个字符串对象
            System.out.println( str1  ==  str2);
             
        }
     
    }
    

    示例 2 : 是否是同一个对象-特例

    str1 = "the light";
    str3 = "the light";

    一般说来,编译器每碰到一个字符串的字面值,就会创建一个新的对象
    所以在第6行会创建了一个新的字符串"the light"
    但是在第7行,编译器发现已经存在现成的"the light",那么就直接拿来使用,而没有进行重复创建

    package character;
     
    public class TestString {
     
        public static void main(String[] args) {
            String str1 = "the light"; //第6行
            String str3 = "the light"; //第7行
            System.out.println( str1  ==  str3);
        }
     
    }
    

    示例 3 : 内容是否相同

    使用equals进行字符串内容的比较,必须大小写一致
    equalsIgnoreCase,忽略大小写判断内容是否一致

    package character;
      
    public class TestString {
      
        public static void main(String[] args) {
      
            String str1 = "the light";
              
            String str2 = new String(str1);
             
            String str3 = str1.toUpperCase();
     
            //==用于判断是否是同一个字符串对象
            System.out.println( str1  ==  str2);
             
            System.out.println(str1.equals(str2));//完全一样返回true
             
            System.out.println(str1.equals(str3));//大小写不一样,返回false
            System.out.println(str1.equalsIgnoreCase(str3));//忽略大小写的比较,返回true
             
        }
      
    }
    

    示例 4 : 是否以子字符串开始或者结束

    startsWith //以...开始
    endsWith //以...结束

    package character;
      
    
    public class TestString {
      
        public static void main(String[] args) {
            String str1 = "the light";
             
            String start = "the";
            String end = "Ight";
             
            System.out.println(str1.startsWith(start));//以...开始
            System.out.println(str1.endsWith(end));//以...结束
              
        }
      
    }
    

    练习比较字符串

    创建一个长度是100的字符串数组
    使用长度是2的随机字符填充该字符串数组
    统计这个字符串数组里重复的字符串有多少种(忽略大小写)
    在这里插入图片描述

    答案

    package character;
     
    public class TestString {
     
        public static void main(String[] args) {
     
            String[] ss = new String[100];
            // 初始化
            for (int i = 0; i < ss.length; i++) {
                ss[i] = randomString(2);
            }
            // 打印
            for (int i = 0; i < ss.length; i++) {
                System.out.print(ss[i] + " ");
                if (19 == i % 20)
                    System.out.println();
            }
     
            for (String s1 : ss) {
                int repeat = 0;
                for (String s2 : ss) {
                    if (s1.equalsIgnoreCase(s2)) {
                        repeat++;
                        if (2 == repeat) {
                            // 当repeat==2的时候,就找打了一个非己的重复字符串
     
                            putIntoDuplicatedArray(s1);
                            break;
                        }
                    }
                }
            }
     
            System.out.printf("总共有 %d种重复的字符串%n", pos);
            if (pos != 0) {
                System.out.println("分别是:");
                for (int i = 0; i < pos; i++) {
                    System.out.print(foundDuplicated[i] + " ");
                }
            }
        }
     
        static String[] foundDuplicated = new String[100];
        static int pos;
     
        private static void putIntoDuplicatedArray(String s) {
            for (int i = 0; i < pos; i++){
                if (foundDuplicated[i].equalsIgnoreCase(s))
                    return;
            }
     
            foundDuplicated[pos++] = s;
        }
     
        private static String randomString(int length) {
            String pool = "";
            for (short i = '0'; i <= '9'; i++) {
                pool += (char) i;
            }
            for (short i = 'a'; i <= 'z'; i++) {
                pool += (char) i;
            }
            for (short i = 'A'; i <= 'Z'; i++) {
                pool += (char) i;
            }
            char cs[] = new char[length];
            for (int i = 0; i < cs.length; i++) {
                int index = (int) (Math.random() * pool.length());
                cs[i] = pool.charAt(index);
            }
            String result = new String(cs);
            return result;
        }
     
    }
    
  • 相关阅读:
    第二月 day 2,内置函数
    第二月 day3 闭包,递归
    day4 装饰器
    第二月 day1生成器
    第一个月 总结
    day 16 迭代器
    day 15 编码
    Docker常用命令
    DRF源码刨析
    django中使用qiniu作为第三方存储
  • 原文地址:https://www.cnblogs.com/jeddzd/p/11627100.html
Copyright © 2020-2023  润新知