Java字符串的應用

字符串的簡單應用java

public class join {
public static void main (String args[]){
    String s1 = new String ("hello");
            String s2 = new String ("word");
            String s = s1  + s2;
            int size= s.length();
            System.out.println(size);
}
}//輸出字符串長度

/*public class join {
public static void main (String args[]){
    String s1 = new String ("hello");
            String s2 = new String ("word");
            String s = s1 + " " + s2; 
            System.out.println(s);
}
}//鏈接多個字符串*/
/*public class join {
public static void main (String args[]){
    String s1 = new String ("hello");
            String s2 = new String ("word");
            String s = s1  + s2;
            int size= s.indexOf("e"); //在這裏能夠修改局部變量 "e" 能夠改變成其餘的浮點
            System.out.println(size);
            }
}//字符串的查找
*/
/*public class join {
public static void main (String args[]){
    String s1 = new String ("hello");
            String s2 = new String ("word");
            String s = s1  + s2;
            int size= s.lastIndexOf(""); //""中間沒有空格表示空字符串
            System.out.println("空字符在字符串中的索引位置"+size);
            System.out.println("字符串的長度是" + s.length()); 
            }
}//查找空字符串的位置並輸出長度*/

鏈接其餘數據類型(Link型正則表達式

public class Link {
public static void main(String args[]) {
    int booktime = 4;
    float practicetime = 2.5f;
    System.out.println( "我天天花費 " + booktime + "小時看書;" + practicetime + "小時練習");
       }
}

索引位置x的對應位置的字母是什麼函數

 

public class join {
public static void main (String args[]){
    String s1 = new String ("hello");
            String s2 = new String ("word");
            String s = s1 + " " + s2; 
            char j =s.charAt(6);
            System.out.println("索引位置6所對的字母是"+j);
                         }
}

 

 獲取字符串spa

public class join {
public static void main (String args[]){
    String s1 = new String ("hello");
            String s2 = new String ("word");
            String s = s1+s2;
            System.out.println(s);
            String y=s.substring(0, 3);
            System.out.println(y);
            }
}

 

去除空格3d

public class join {
public static void main (String args[]){
    String s1 = new String ("hello");
            String s2 = new String ("word");
            String s = "  "+s1+"   "+s2+"   ";
            System.out.println("原來的字符串長度爲"+s.length());
            System.out.println("去掉空格後的字符串長度爲"+s.trim().length());
            }
}

 

 字符串的替換code

public class join {
public static void main (String args[]){
    String s1 = new String ("hello");
            String s2 = new String ("word");
            String s = "  "+s1+"   "+s2+"   ";
            System.out.println(s);
            System.out.println("替換以後的"+s.replace("word", "java"));
            }
}

替換以後是hello  javaorm

判斷字符串的開始和結尾blog

public class startorend {
public static void main(String args[]) {
String num1="12345678";
String num2="23456789";
boolean b=num1.startsWith("12");
boolean c=num1.endsWith("89");
boolean d=num2.startsWith("12");
boolean e=num2.endsWith("89");
System.out.println("num1輸出的是否以12開始"+b);
System.out.println("num1輸出的是否以89結束"+c);
System.out.println("num2輸出的是否以12開始"+d);
System.out.println("num1輸出的是否以89結束"+e);
}
    public startorend() {
        // TODO 自動生成的構造函數存根
    }

}

 

用equal 和equalignore判斷字符串是否相等索引

public class Opinion {
public static void main(String args[]) {
    String a=new String("abc");
    String b=new String ("ABC");
    String c=new String ("abc");
    boolean x=a.equals(b);
    boolean y=a.equalsIgnoreCase(b);
    System.out.println(x);
    System.out.println(y);
}
    public Opinion() {
        // TODO 自動生成的構造函數存根
    }

}

 

按照字典順序比較兩個字符串圖片

public class Opinion{
    public static void main(String args[]) {
    String s1="a";
    String s2="b";
    String s3="c";
    System.out.println(s1 +" "+"comepareTo"+" "+s2+s1.compareTo(s2));
    System.out.println(s1+" "+"comepareTo"+" "+s3+s1.compareTo(s3));
    
    }
}

 

字符串字母大小寫轉換

public class Opinion{
    public static void main(String args[]) {
    String s1="abc";
    String s2="LZB";
         String s=s1+s2;
 String news1=s.toLowerCase();
 String news2=s.toUpperCase();
    System.out.println(s);
    System.out.println(news1+news2);
    }
}

 

日期和時間字符串格式化 

import java.util.Date;
@SuppressWarnings("unused")
public class date {
public static void main(String[] args) {
    Date days=new Date();//建立DATE的類day
    String year =String.format("%tY", days);
    String month=String.format("%tB", days);
    String day=String.format("%td", days);
    System.out.println("今年是"+year+"年");
    System.out.println("如今是"+month);
    System.out.println("今天是"+day+"號");
}
    public date() {
        // TODO 自動生成的構造函數存根
    }

}

 

 時間格式化

import java.util.Date;
public class date {
public static void main(String[] args) {
    Date time=new Date();//建立DATE的類time
    String hour =String.format("%tH", time);
    String minute=String.format("%tM", time);
    String second=String.format("%tS", time);
    System.out.println("如今的時間是"+hour+"時"+minute+"分"+second+"秒");
}
    public date() {
        // TODO 自動生成的構造函數存根
    }

}
import java.util.Date;
public class date {
public static void main(String[] args) {
    Date days=new Date();//建立DATE的類time
    String day =String.format("%tc", days);
    String form=String.format("%tF", days);
    System.out.println("所有的時間信息是"+day);
    System.out.println("年-月-日格式"+form);
}
    public date() {
        // TODO 自動生成的構造函數存根
    }

}
實驗結果:
所有的時間信息是星期二 二月 04 09:54:45 CST 2020
年-月-日格式2020-02-04

常規類型格式化

public class Opinion{
    public static void main(String args[]) {
    String s1=String.format("%d", 400/2);
    String s2=String.format("%b", 2>7);
 String s=String.format("%x", 100);
    System.out.println("2>7"+s2);
    System.out.println("400的一半是"+s1);
    System.out.println("100的十六進制數是"+s);
    }
}

正則表達式判斷郵箱是否合法

 

public class Opinion{
    public static void main(String args[]) {
    //定義正確的qq郵箱格式
    String regex="\\w+@\\w+(\\.\\w{2,3})";//"\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}}";
    String s1="aaaa@.com";
    String s2="aaaa.23a";
    String s3="2358903351@qq.com";
    if (s1.matches(regex)) {
        System.out.println(s1+"是合法的");
    }
    if(s2.matches(regex)) {
        System.out.println(s2+"是合法的");
    }
    if(s3.matches(regex)) {
        System.out.println(s3+"是合法的");
    }
    }
}

小編第一次寫這麼多,圖片那個胖胖的手指!

不要注意!

不要注意!

不要注意!

若是看到了。。。。。我攤牌了沒錯就是我!

若是對初學者有幫助,別忘了快誇誇我呀!!!

相關文章
相關標籤/搜索