java字串問題

2021-09-05 01:16:27 字數 2246 閱讀 4940

1.stringutils中 isnotempty 和isnotblank的區別:

isnotempty(str)等價於 str != null && str.length > 0

isnotblank(str) 等價於 str != null && str.length > 0 && str.trim().length > 0//trim()方法的作用是去掉字串頭尾的空格

同理isempty 等價於 str == null || str.length == 0

isblank 等價於 str == null || str.length == 0 || str.trim().length == 0

str.length > 0 && str.trim().length > 0 ---> str.length > 0

2.string和date之間的轉換

******dateformat formatter = new ******dateformat("dd-mmm-yyyy");

string dateinstring = "7-nov-2018";

try catch (parseexception e)

3.將string 轉化成long

long l = long.parselong(hahanihao);//hahanihao如果是空,會報空指標錯誤
4.decimalformat數字格式化

decimalformat df1 = new decimalformat("0.0"); 

decimalformat df2 = new decimalformat("#.#");

decimalformat df3 = new decimalformat("000.000");

decimalformat df4 = new decimalformat("###.###");

system.out.println(df1.format(12.34)); //結果: 12.3

system.out.println(df2.format(12.34));

system.out.println(df3.format(12.34)); // 012.340

system.out.println(df4.format(12.34)); // 12.34

5.oracle中bigdecimal的int、string相互轉換

int或者string轉bigdecimal:

string money = 12.34; //字串型

bigdecimal moneys =new bigdecimal(money); //宣告

int moneys1 = moneys.intvalue(); //轉換int

string moneys2 = moneys.tostring(); //轉換string

//bigdecimal乘法(bigdecimal型別只能與bigdecimal型別相乘)

bigdecimal prices=new bigdecimal(price).multiply(new bigdecimal(100));

//設定小數字,變數1是小數字數,變數2是取捨方法(四捨五入)

bigdecimal prices1=prices.setscale(2, bigdecimal.round_half_up);

//直接設定某個值的話:

***.set***(new bigdecimal(***));

bigdecimal 轉int或者string:

***.tostring

6.bigdecimal比較選擇equals還是compareto方法

equals方法會比較值和精確度,而compareto則會忽略精度

public static void main(string args) }

//-------------例子2:

public boolean yearcheck(bigdecimal year, string typecode) else

} }return result;

}

java 字串轉化整型問題

public class stringparesinteger 本題考查的主要是邊界條件 1.穿入的字串是否為空 2.字串的首位是否為 3.字元中是否有非法字元 4.穿入的字串是否超過了整數的最大值 integer.max value 2147483647 integer.min value 214...

java的字串相關問題

1 string 和stringbuffer stringbuilder 的區別 string 字串常量,一旦string物件被建立就不可以被修改,而例如 string str aaa str str abc 後面的str時新建立的物件,原來的str物件用完就會被gc 這樣修改字串使用的時間會很長。...

java字串替換的問題

今天工作中遇到乙個問題,需要輸出mongodbobject中的部分內容,當我轉換成字串以後出現了好像無法替換的問題,經過驗證,發現並非是不能替換,只是想法錯誤而已。package demo 輸出字串中的chenggongzhimu,shibai,count,123 author tuzongxun ...