JAVA 常用類的學習 8 30

2021-08-26 12:35:13 字數 2773 閱讀 5474

構建字串的方式

string s1 = new string("hello");

char cs = ;

string s2 = new string(cs);

string s3 = "hello"; // 最常用

常用的方法
public

class demo1 ;

string s2 = new string(cs);

string s3 = "hello"; // 最常用

system.out.println(s1);

system.out.println(s2);

system.out.println(s3);

system.out.println(s3.charat(1));

string s4 = s3.concat(",world!");

system.out.println(s4);

system.out.println(s4.contains("world1"));

string s5 = "d:/a/index.js";

system.out.println(s5.endswith(".js"));

// 對於字串的比較操作,一般的場景都是比較字串的內容是否一致

string s6 = "tom";

string s7 = new string("tom");

system.out.println(s6 == s7); // false

system.out.println(s6.equals(s7)); // true

string s8 = "hello,world!";

system.out.println(s8.indexof("world")); // 6

string s9 = "a1a2a3b4a5";

system.out.println(s9.indexof("a")); // 0

system.out.println(s9.indexof("a", 1)); // 2

listindexs = getindexsofstr("1a22a3444a4b", 'a');

for (integer index : indexs)

// 列印檔案的字尾

string s10 = "d:/a/index.js";

int s10_1 = s10.lastindexof(".") + 1;

string s10_2 = s10.substring(s10_1);

system.out.println(s10_2);

string s11 = "abcabcabc";

system.out.println(s11.replace("a", "a"));

system.out.println(s11.replacefirst("a", "a"));

string s12 = "a-b-c-d-e-f";

string s12s = s12.split("-");

for (string s : s12s)

string s13 = "20171108001";

system.out.println(s13.startswith("20171108"));

system.out.println(s12.touppercase());

string s14 = " hello,wolrd! ";

string s15 = s14.trim();

system.out.println("#"+s14+"#");

system.out.println("#"+s15+"#");

}// 傳入乙個字串,輸出某個字元在傳入字串中的所有位置

public

static listgetindexsofstr(string src, char c) }}

return rs;

}// 傳入乙個字串,輸出某個字元在傳入字串中的所有位置

public

static listgetindexsofstr(string src, string c)

return rs;

}}

public

class demo3

date d22 = new date();

system.out.println(d22.gettime() - d11.gettime()); // 3418

date d1 = new date();

stringbuffer sb = new stringbuffer("");

for (int i = 0; i < 100000; i++)

date d2 = new date();

system.out.println(d2.gettime() - d1.gettime()); // 16

}}

date 和 ******dateformat 的運用
public

class demo4

}

calendar類

calendar 是更加先進的日期處理類

如果只是簡單的對日期進行輸入,用 date 就可以了,但是如果要對日期進行複雜的運算,就要使用 calendar 類,calendar 和 date 是可以互相轉化的。

public class demo5 

}

Java學習 常用類(StringBuilder)

四 替換 replace 五 刪除 delete 六 查詢 七 轉換 八 反轉 reverse 九 比較 equals 或 stringbuilder 字串變數 非執行緒安全,但速度快 常用。stringbuffer 字串變數 執行緒安全,但速度慢 string 字串不可變。stringbuilde...

Java學習之java中的常用類

一 包裝類 1.將本型別和其它型別進行轉換的方法。2.將字串和本型別及包裝類互相轉換的方法。以integer 包裝類為例 integer包裝類的構造方法 int s 1 integer s1 new integer s 用integer表示變數s的值 double s2 s1.doublevalue...

java常用的類

1 system.in.read 把控制台使用者輸入的 字元流,返回成ascii碼 即為乙個int整數 比如使用者在控制台輸入5,那麼read返回的結果是5的ascii碼53.2 system.in是很簡陋 很原始的輸入流物件,通常不使用它來 讀取使用者的輸入,一般會在外面封裝過濾流,buffere...