小白的java學習之路 “ 字串”

來源:互聯網
上載者:User

標籤:必須   使用者   輸入密碼   一個   電話   practice   public   AC   常用   

定一個字串可以使用string類和stringbuffer類。

string類提供了大量的操作字串的方法,常用的如下:

獲得字串的長度: length()。

比較字串:equals()。

連結字串:concat()。

提取字串:substring()。

搜尋字串:indexOf()。

拆分字串:spilt()。

常用的stringbuffer類提供的方法如下:

轉換成string類型:tostring()。

連結字串:append()。

插入字串:intsert()。

一些案例:

public class Demo_1 {    public static void main(String[] args) {        // TODO Auto-generated method stub        String pwd;        @SuppressWarnings("resource")        Scanner input =new Scanner(System.in);        do{        System.out.println("請輸入使用者名稱:");        String name = input.next();        System.out.println("請輸入密碼:");        pwd= input.next();        if(pwd.length()<6){            System.out.println("密碼長度不夠");        }else{            System.out.println("輸入正確");            System.out.println("歡迎你!"+name);            break;        }        }while(pwd.length()!=6);    }
boolean flag ;    public  void panduan(String sfz , String sjh,String zj) {        flag=false;        int index=sjh.lastIndexOf("-");            if(!(sfz.length()==16||sfz.length()==18)){        System.out.println("社會安全號碼必須是16位或18位!");    }    else if(sjh.length()<11){        System.out.println("手機號必須是11位!");    }else if (index!=-1&&index!=0&&index!=4&&zj.length()!=13) {        System.out.println("有線電話號碼區必須為4位,電話號碼必須是7位!");    }else{    System.out.println("註冊成功");    flag=true;          }    }調用:public static void main(String[] args) {        @SuppressWarnings("resource")        Scanner input=new Scanner(System.in);        Practice2 pd =new Practice2();        do {            System.out.println("請輸入身份證:");            String sfz = input.next();            System.out.println("請輸入手機號碼:");            String sjh = input.next();            System.out.println("請輸入有線電話號:");            String zj = input.next();            pd.panduan(sfz, sjh, zj);        } while (pd.flag==false);    }

 

小白的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.