当前位置: 移动技术网 > IT编程>开发语言>Java > Java自学-数字与字符串 比较字符串

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

2019年10月06日  | 移动技术网IT编程  | 我要评论

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;
    }
 
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网