Java基礎之常用類及方法(七)

來源:互聯網
上載者:User

標籤:java   常用類及方法   

String類:

java.lang.String類代表不可變的字元序列。

String類的常見構造方法:

String(String original):建立一個String對象為original的拷貝。

String(char[] value):用一個字元數組建立一個String對象。

String(char[] value,int offset,int count):用一個字元數組從offset項開始的count個字元序列建立一個String對象。

package MyString;

public class Test {

public static void main(String[] args) {

// TODO Auto-generated method stub

String s1="hello";

String s2="world";

String s3="hello";

System.out.println(s1==s3);

s1=new String("hello");

s2=new String("hello");

System.out.println(s1==s2);

System.out.println(s1.equals(s2));

char c[]={‘s‘,‘u‘,‘n‘,‘ ‘,‘j‘,‘a‘,‘v‘,‘a‘};

String s4=new String(c);

String s5=new String(c,4,4);

System.out.println(s4);

System.out.println(s5);

}

}

String類常用方法(1):

public char charAt(int index):返回字串第index個字元;

public int length():返回字串的長度;

public int indexof(String str):返回字串中出現str的第一個位置;

public int indexof(String str,int fromIndex):返回字串中從fromIndex開始出現str的第一個位置;

public boolean equalsIgnoreCase(String another):比較字串與another是否一樣(忽略大小寫);

public String replace(char oldChar,char newChar):在字串中用newChar字元替換oldChar字元。

package MyString;

public class TestStringMethod {

public static void main(String[] args) {

// TODO Auto-generated method stub


String s1="sun javava";

String s2="Sun Javava";

System.out.println(s1.charAt(1));

System.out.println(s2.length());

System.out.println(s1.indexOf(‘a‘));

System.out.println(s1.indexOf("ava"));

System.out.println(s1.indexOf(‘a‘, 7));

System.out.println(s1.equals(s2));

System.out.println(s1.equalsIgnoreCase(s2));

String s="我是程式員,我在學java";

   String sr=s.replace(‘我‘,‘你‘);

   String si="[IP:10]";

   String sr1=si.replace(‘[‘, ‘ ‘);

   String sr2=sr1.replace(‘]‘,‘ ‘);

   System.out.println(sr);

   System.out.println(sr1);

   System.out.println(sr2);

}

}

String類常用方法(2)

public boolean startsWith(String prefix):判斷字串是否以prefix字串開頭;

public boolean endsWith(String suffix):判斷字串是否以prefix字串結尾;

public String toUpperCase():返回一個字串為該字串的大寫形式;

public String toLowerCase():返回一個字串為該字串的小寫形式;

public String substring(int beginIndex):返回該字串從beginIndex開始到結尾的子字串;

public String substring(int beginIndex,int endIndex):返回該字串從beginIndex開始到endIndex結尾的子字串;

public String trim():返回將該字串去掉開頭和結尾空格後的字串。

package MyString;

public class TestStringMethodO {

public static void main(String[] args) {

// TODO Auto-generated method stub

String s="Welcome to Java World!";

String s1="   sun java    ";

String s2="[IP1:10]";

System.out.println(s.startsWith("Welcome"));

System.out.println(s.endsWith("orld"));

String sL=s.toLowerCase();

String sU=s.toUpperCase();

System.out.println(sL);

System.out.println(sU);

String subS=s.substring(11);

String subS1=s.substring(0, 6);

String subS2=s.substring(0, 7);

System.out.println(subS);

System.out.println(subS1);

System.out.println(subS2);

String sp=s1.trim();

String s3=s2.replace(‘[‘, ‘ ‘);

String s4=s3.replace(‘]‘, ‘ ‘);

String s5=s4.trim();

System.out.println(sp);

System.out.println(s4);

System.out.println(s5);

}

}


本文出自 “一步,一步” 部落格,請務必保留此出處http://summerflowers.blog.51cto.com/5202033/1921727

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.