java:字串(下)

來源:互聯網
上載者:User

標籤:===   compare   asd   form   cond   day   als   mem   array   

判斷字串是否相等

對字串對象進行比較不能簡單地比較子“====”,因為比較子比較的是兩個字串的地址是否相同。即使兩個字串的內容相同。

即使兩個字串的內容相同,兩個對象的記憶體位址也是不同的,使用比較子仍然會返回false。

equals()方法和equalsIgnoreCase方法

如果兩個字串具有相同的字元和長度,則使用equals方法()進行比較時,返回ture

使用equals()方法對字串進行比較時是區分大小寫,而使用equalsIgnoreCase()方法是忽略了大小寫情況下比較兩個字串是否相等,返回結果仍未boolean類型。

我們用一組代碼來進行比較

public class Opinion { public static void main(String[] args) {
  String s1=new String("abc");
  String s2=new String("ABC");
  boolean b=s1.equals(s2);//定義b為布爾類型s1和s2是否相同
        boolean b2=s1.equalsIgnoreCase(s2);//定義b2為布爾類型s1和s2是否相等
        System.out.println(s1+"\t"+"equals"+"\t"+s2+"\t"+b);//列印
        System.out.println(s1+ "\t"+"equalslgnoreCase"+"\t" +s2+":"+b2);
 }}執行結果:abc equals ABC false
abc equalslgnoreCase ABC:true按字典順序比較兩個字串CompareTo()方法為按字典順序比較兩個字串,該比較基於字串中各個字元的unicode值,按字典順序將此String對象表示的字元序列與參數字串所表示的字串序列進行比較如果按字典順序Sting對象表示的字串序列進行比較。如果按字典順序此String對象位於參數字串之前,則比較結果為一個負整數;如果按字典順序此String 對象位於參數數字字串之後,則比價結果為一個正整數了;如果這兩個字串相等則結果為0使用equals()方法比較,進行輸出public class Wordbook { public static void main(String[] args) {
  String str=new String("b");
  String str2=new String("a");
  String str3=new String("c");
  System.out.println(str+"compareTO"+str2+"."+str.compareTo(str2));
        System.out.println(str+"compareTo"+str3+"."+str.compareTo(str3));
 }}執行結果為:bcompareTOa.1
bcompareToc.-1字母大小寫轉換:字串的tolowerCase()方法課將字串中的所有字元從大寫字母改為小寫字母,而toUpperCase()方法可將字串中的小寫字母改為大寫字母。toLowerCase()是轉換成小寫,toUpperCase()是轉換成大寫public class UpAndLower { public static void main(String[] args) {
  String str=new String("abcDEF");//str定義新的字串類型
  String newstr=str.toLowerCase();//將str中的abcdef轉換為小寫
        String newstr2=str.toUpperCase();//將str中的abcde轉換為大寫
        System.out.println(newstr);//列印newstr
        System.out.println(newstr2);//列印newstr2
 }}執行結果為:abcdef
ABCDEF字串分割:使用split()方法可以是字串按指定的分割字元或字串對內容進行分割,並將分割後的結果存放在字串數組中。public class Division { public static void main(String[] args) {
  String str="192.168.1.1";
  String[] firstArray=str.split("\\.");
        String[] secondArray=str.split("\\.",2);
        System.out.println("str的原值為:["+str+"]");
        System.out.println("全部分割結果為:");
        for(String a:firstArray){
         System.out.print("["+a+"]");
        }
        System.out.println();
        System.out.println("分割兩次結果為");
        for(String a:secondArray){
         System.out.print("["+a+"]");
        }
        System.out.println();
 }
}執行結果為:str的原值為:[192.168.1.1]
全部分割結果為:
[192][168][1][1]
分割兩次結果為
[192][168.1.1]格式化字串String類靜態forma格式化的字串方法用於建立格式化的字串format()方法有兩種重載形式public class All_所有時間 {
   public static void main(String[] args) {
 Date date=new Date();
 String year=String.format("%tY", date);
 String month=String.format("%tm",date);
 String day=String.format("%td", date);
 String a=String.format("%ta", date);
 String hour=String.format("%tH", date);
 String minute=String.format("%tM", date);
 String second=String.format("%tS", date);
 System.out.println(year+"/"+month+"/"+day+"  "+a+"   "+hour+":"+minute+":"+second);
}
}執行結果為:2018/07/05  星期四   15:00:17使用Regex:Regex用於判斷語句居多,用來檢查某一字串時候滿足某一格式用判斷E-mai地址合法的是那幾個public class Judge { public static void main(String[] args) {
  //定義要匹配的E-mail地址的Regex
  String regex="\\[email protected]\\w+(\\.\\w{2,3})*\\.\\w{2,3}";//符合這個Regex的為E-mail地址
  String str1="[email protected]";
  String str2="[email protected]";
  String str3="[email protected]";
        if(str1.matches(regex)){
         System.out.println(str1+"是一個合法的E-mail地址格式");
        }
        if(str2.matches(regex)){
         System.out.println(str2+"是一個合法的E-mail地址格式");
        }
        if(str3.matches(regex)){
         System.out.println(str3+"是一個合法的E-mail地址格式");
        }
 }
}執行結果為:[email protected]是一個合法的E-mail地址格式
[email protected]是一個合法的E-mail地址格式字串產生器:建立成功的字串,長度為固定的,內容不能被改變和編譯。下面就來驗證一下字串的長度和其字串的工作效率public class Jreque { public static void main(String[] args) {
  String str="";
  //定義對字串執行操作的起始時間
  long startTime=System.currentTimeMillis();
  for(int i=0;i<10000;i++){
   str=str+i;
  }
  long endTime=System.currentTimeMillis();
  long time=endTime-startTime;
  System.out.println("String消耗時間"+time);
  StringBuilder builder=new StringBuilder("");
  startTime=System.currentTimeMillis();
  for(int j=0; j<10000;j++){
   builder.append(j);
  }
  endTime=System.currentTimeMillis();
  time=endTime-startTime;
  System.out.println("StringBuilder 消耗時間:"+time);
 }}執行結果為:String消耗時間273
StringBuilder 消耗時間:1

java:字串(下)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.