1. 程式人生 > >小白的java學習之路 “ 字符串”

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

輸入 bsp 轉換成 con substring todo 字符 void inpu

定一個字符串可以使用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學習之路 “ 字符串”