Java1---java工具類的使用學習(未完成)
阿新 • • 發佈:2018-11-01
導讀
1.簡介
2.Math類
3.String類
簡介
java工具類原碼檢視方法:
(1)官網
(2)jdk-> src->src.zip
(3)eclipse command+單機 跳轉到原始碼
Math類
->java.lang(預設)
Math類的函式都是靜態的 /* 求絕對值 a可以是int, long, float, double @return 與a相同型別 */ Math.abs(a) /* 天花板數 ,向上取整 a是double @return 與a相同型別 double */ Math.ceil(a) /* 地板數, 向下取整 a是double @return 與a相同型別 double */ Math.floor(a) /* 求最大最小值 a,b可以是int, long, float, double @return 與a,b相同型別 */ Math.max(a,b) Math.min(a,b) /* 求a的b次方 a,b 是double @return double */ Math.pow(a,b) /* 四捨五入 a是float,double @return float->int double->long */ Math.round(a) /* 產生隨機數【0,1) @return double */ Math.random() //產生【0,10】(0~10) System.out.println((int)(Math.random()*11)); //產生【5,10】(5~10) System.out.println((int)(Math.random()*6+5));
String類
->java.lang
String value;
/*
(非靜態)返回索引處的char值
index int
@return char
*/
value.charAt(index);
/*
(非靜態) 將引數字串連線到物件後邊並返回新的字串,原字串沒有變化
string String
@return String
*/
value.concat(String string);
/*
1.(非靜態)判斷物件是否包含引數字串
2.string CharSequence
->CharSequence與String都能用於定義字串,但CharSequence的值是可讀可寫序列,而String的值是隻讀序列。
CharSequence是實現這個介面的例項 舉例: CharSequence str = "dd"; 就是 CharSequence str = new String("dd");
CharSequence是封裝好的類
這是一個介面,代表的是一個有序字元集合,這個介面包含的方法有:charAt(int index),toString(),length(),subSequence(int start,int end).
這裡需要說的一點就是,對於一個抽象類或者是介面類,不能使用new來進行賦值,但是可以通過以下的方式來進行例項的建立:
CharSequence cs="hello";
但是不能這樣來建立:
CharSequence cs=new CharSequence("hello");
[email protected] boolean
*/
value.contains(string);
/*
1.(非靜態)判斷字串是否以引數字串結尾或開頭
2.string String
[email protected] boolean
*/
value.endsWith(string);
value.startsWith(string);
/*
1.(非靜態)字串長度
[email protected] int
*/
value.length();
/*
1.(非靜態)判斷與引數內容是否相等
2.string Object
[email protected] boolean
*/
value.equals(string);
/*
1.(非靜態)尋找匹配串的第一次或最後一次出現位置索引
2.string String,char,int
[email protected] int
*/
value.indexOf(string);
value.lastIndexOf(string);
/*
1.(非靜態)從指定位置開始找匹配串第一次出現的位置索引
2.(1)string String,char,int (2)from Index int
[email protected] int
*/
value.indexOf(string,fromIndex);
value.lastIndexOf(string,fromIndex);
/*
1.(非靜態)字串是否為空
[email protected] boolean
*/
value.isEmpty();
/*
1.(非靜態)將物件字串中old替換為new,並輸出,原字串不變
2.(1)old char,CharSequence (2)new char,CharSequence
[email protected] String
*/
value.replace(old,new);
/*
1.(非靜態)將字母轉化為小寫或大寫
[email protected] String
*/
value.toLowCase();
value.toUpprCase();
/*
1.(非靜態)忽略大小寫的字串比較
2.string String
[email protected] boolean
*/
value.equalsIgnoreCase(string);
/*
1.(非靜態)按照給定的字串拆分物件字串
eg: string= hellowefjworld ,結果:hello efj orld
2.string String
[email protected] String[]
*/
value.split(string);
/*
1.(非靜態)去掉物件字串的前導空白和尾部空白
eg: value=" hellow world " 結果為“hello world”
[email protected] String
*/
value.trim();
/*
1.(非靜態)把字串轉化為陣列
[email protected] char[]
*/
value.toCharArray();
/*
1.(靜態)把其他型別轉化為字串型別
2.string boolean,int,long,double,float,char,char[],object
除了這個方法,將基本型別轉為字串還可以如下操作:
String value=num+"";
[email protected] String
*/
String.valueOf(string);