java 字串操作和日期操作,java字串

來源:互聯網
上載者:User

java 字串操作和日期操作,java字串

一、字串操作

建立字串

String s2 = new String("Hello World");

String s1 = "Hello World";

 

1.字串串連

多個字串連結時,每個字串之間用+相連,+就是字串連結,串連之後產生一個新的字串。

2.擷取字串長度 a.lenght()  

根據索引從0開始,截取字串長度 a.substring(1,3) ; 從1號位開始截取到3號位。

3.擷取指定字串的索引位置 indexOf()方法;lastIndexOf()方法。

indexOf(s)傳回值:傳回值字串中第一次出現s的索引

lastIndexOf(s)傳回值:返回字串中s最後一次出現s的索引

4.去除字串前、後空格 trim()

5.替換所有與制定字串相匹配的字串replace()方法

6.判斷字串是否相等equals()方法

使用equals()對字串進行比較時嚴格區分大小寫,在此條件下,如果兩個字串仍具有相同的字元和長度,則返回true,不相同則返回false。

7。判斷字串的開始startsWith()方法判斷字串的結尾endsWith()方法

法用於判斷當前字串對象是否以參數制定的字元開始或結束。

8.大小寫轉換

將字串中的大寫字母轉換為小寫toLowerCase()方法;

將字串中的小寫字母轉換為大寫toUpperCase()方法.

9.字串分割split(String sign)方法

該方法根據制定的分隔字元對字串進行完全分割。

作業:

    public static void main1 (String[] args){        String a = "abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" ;        for(int i=0;i<4;i++){        int b =    (int)(Math.random()*100)%a.length();        String c = a.substring(b, b+1);        System.out.print(a.substring(b, b+1));                        }    }//隨機產生不重複的四位元的驗證碼

public static void main(String[] args){        String a = " <student><xm>張三</xm><xb>男</xb></student>";        String b = a.substring(a.indexOf("<xm>")+4,a.indexOf("</xm>") );        System.out.println("姓名:"+b);        String c =a.substring(a.indexOf("<xb>")+4,a.indexOf("</xb>") );        System.out.println("性別:"+c);            }  //截取姓名和性別    

 

二、日期操作:Calendar

(一)讀日期
Calendar a = Calendar.getInstance();
d.get(常量);
d.get(Calendar.YEAR);//返回數字年
d.get(Calendar.MONTH);//月
d.get(Calendar.DAY_OF_MONTH);//日
d.get(Calendar.HOUR);//時
d.get(Calendar.MINUTE);//分
d.get(Calendar.SECOND);//秒
d.get(Calendar.MILLISECOND);//毫秒

日期的格式化顯示:

使用日期格式化顯示器 SimpleDateFormat
1.造日期的Calendar
Calendar a = Calendar.getInstance();

2.造格式化器
SimpleDateFormat f = new SimpleDateFormat("格式化樣式");
yy,yyyy --年
M,MM--月
d,dd --天
h,hh--時,12小時制; HH--時,24小時制
m,mm-分
s,ss-秒

3.對calendar進行格式化

f.format(日期);   //注意,是Date不是Calendar;

f.format(a.getTime());    //使用Calendar對象的getTime()函數,轉換成Date對象

(二)寫日期
Calendar a = Calendar.getInstance();
a.set(年,月,日);
a.set(年,月,日,時,分,秒);
a.set(常量,值);
//c.set(1999,8,12);
//c.set(1999, 2,4,18,55,32);
//c.set(Calendar.YEAR, 1980);

public class Dog {    public static void main(String[] args){        Calendar a = Calendar.getInstance();        a.set(2002, 2, 13);        SimpleDateFormat b = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");        String c = b.format(a.getTime());        System.out.println(c);            }//格式化日期顯示
//時分秒不寫是目前時間

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.