java基礎(9)

來源:互聯網
上載者:User

標籤:姓名   strong   last   變數   使用者名稱   大小寫   存在   步驟   splay   

一、字串類String

1、String是一個類,位於java.lang包中

2、建立一個字串對象的2種方式:

                   String 變數名=“值”;

                   String 對象名=new String(“值”);

3、字串的常用方法

                   3.1 字串長度——length()

                   3.2 判斷值是否相等 equals()

                   3.3 判斷字串對象地址是否相同 ==

                   3.4 忽略 大小寫 判斷 equalsIgnoreCase()

                   3.5 大小寫轉換

                                           toLowerCase()——小寫

                   toUpperCase()——大寫

public static void main(String [] args){        Scanner input=new Scanner(System.in);        String uname,pwd;        System.out.print("請輸入使用者名稱:");        uname =input.next();        System.out.print("請輸入密碼:");        pwd =input.next();        if(pwd.length()>=6){            System.out.println("註冊成功!");        }else{            System.out.println("密碼長度不能小於6位!");        }    }
判斷長度
public static void main(String [] args){        Scanner input=new Scanner(System.in);        String uname,pwd;        System.out.print("請輸入使用者名稱:");        uname =input.next();        System.out.print("請輸入密碼:");        pwd =input.next();        if (uname.equals("tom") && pwd.equals(" 1234567")){            System.out.println("登陸成功!");        }else{            System.out.println("密碼或帳號錯誤!");        }    }
V判斷字串是否相等
public static void main(String [] args){        Scanner input=new Scanner(System.in);        String Str1=new String("我愛我祖國");        String Str2=new String("我愛我祖國");        if(Str1.equals(Str2)){            System.out.println("兩個字串相等");        }else {            System.out.println("兩個字串不相等");        }        if(Str1==Str2){            System.out.println("兩個字串相等");        }else {            System.out.println("兩個字串不相等");        }        }
判斷字串地址是否相同

 

        3.6 字串串連的方式

                   方式1:使用+號

                   方式2:字串變數.concat(要串連的字串);

        3.7 字串提取的常用方法

                        同一個方法,有多種表現形式——方法重載

3.7.1 indexOf(字元或字串),尋找字元或字串首次出現的位置;

返回一個數字,-1表示沒有,返回是>=0的數字,表示存在

3.7.2 lastIndexOf(字元或字串),尋找字元或字串最後一次出現的位置;

返回一個數字,-1表示沒有,返回是>=0的數字,表示存在

3.7.3 substring()方法

l  substring(提取位置):表示從提取位置開始一直提取到字串的末尾結束

l  substring(開始位置,結束位置):表示提取從開始位置,到結束位置-1的子字串[結束位置大於整個字串的長度,出現異常]

3.8 trim()——去掉字串左右兩邊的空格

public static void main(String [] args){        Scanner input=new Scanner(System.in);        System.out.print("請輸入使用者姓名:");        String name=input.next();        System.out.print("請輸入使用者密碼:");        String pwd=input.next();        if(name.toLowerCase().equals(("TOM").toLowerCase())&&                pwd.toUpperCase().equals(("1234567").toUpperCase())){            System.out.println("登陸成功!");        }else{            System.out.println("使用者名稱或者密碼不匹配,登陸失敗。");        }    }

 

二、StringBuffer類

1、StringBuffer和String的區別?

             String空間或長度是固定的,StringBuffer空間可變

             String效率低於StringBuffer類

  2、使用StringBuffer類的步驟?

        步驟1:先建立對象

             StringBuffer 對象名=new StringBuffer();

StringBuffer 對象名=new StringBuffer(預設值);

        步驟2:向對象中追加或插入值

             StringBuffer對象名.append(追加的值);//可以追加任何值(char,int等)

StringBuffer對象名.insert(要插入的位置,插入的值);

    public static void main(String [] args){        String words="長亭外 古道邊 芳草碧連天 晚風拂 柳笛聲殘 夕陽山外山";        String [] a=new String [50];        System.out.println("***原歌詞***\n"+words);        a=words.split(" ");        for(int i=0;i<a.length;i++){            System.out.println(a[i]);//列印輸出。        }    }

 

java基礎(9)

聯繫我們

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