java中遇到過的String的一些特性

來源:互聯網
上載者:User

標籤:

1.string對象是final的?
1 String str="asdfdf";2 str.replace("as","");3 System.out.println(str);//asdfdf4 str=str.replace("as","");5 System.out.println(str);//dfdf
2.字串的切割

遇到根據url擷取參數的問題:

enterId = Long.parseLong(target.split("/")[2]);
這個要處理切割字串問題
splitapi裡是這樣的:

public String[] split(String regex,int limit)根據匹配給定的Regex來拆分此字串。
此方法返回的數組包含此字串的子字串,每個子字串都由另一個匹配給定運算式的子字串終止,或者由此字串末尾終止。數組中的子字串按它們在此字串中出現的順序排列。如果運算式不匹配輸入的任何部分,那麼所得數組只具有一個元素,即此字串。limit 參數控制模式應用的次數,因此影響所得數組的長度。如果該限制 n 大於 0,則模式將被最多應用 n - 1 次,數組的長度將不會大於 n,而且數組的最後一項將包含所有超出最後匹配的定界符的輸入。如果 n 為非正,那麼模式將被應用儘可能多的次數,而且數組可以是任何長度。如果 n 為 0,那麼模式將被應用儘可能多的次數,數組可以是任何長度,並且結尾Null 字元串將被丟棄。

例如,字串 "boo:and:foo" 使用這些參數可產生以下結果:

Regex Limit 結果
: 2 { "boo", "and:foo" }
: 5 { "boo", "and", "foo" }
: -2 { "boo", "and", "foo" }
o 5 { "b", "", ":and:f", "", "" }
o -2 { "b", "", ":and:f", "", "" }
o 0 { "b", "", ":and:f" }

public String[] split(String regex)根據給定Regex的匹配拆分此字串。 該方法的作用就像是使用給定的運算式和限制參數 0 來調用兩參數 split 方法。因此,所得數組中不包括結尾Null 字元串。

例如,字串 "boo:and:foo" 使用這些運算式可產生以下結果:

Regex 結果
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }


參數:
regex - 定界Regex
返回:
字串數組,它是根據給定Regex的匹配拆分此字串確定的
拋出:
PatternSyntaxException - 如果Regex的文法無效

3.對json字串去空值

上傳代碼錯誤導致資料庫中地址儲存有問題,有一些空的value,還有結尾多了兩個索引值對,當然也有的沒問題。對sql掌握比較差,於是選擇了用java處理,更新資料庫。

處理的資料對象是這樣的:

1 ‘{\"showImg_1\":\"/upload/webHome/template/4ddedebb-7615-4686-b27a-b515a6a5a2cc.jpg\",\"showImg_2\":\"/upload/webHome/template/2c0806ee-6165-4b95-949d-340ddad18171.jpg\",\"showImg_3\":\"/upload/webHome/template/35464a57-a987-4e40-91b4-f5b33e343f46.jpg\",\"showImg_4\":\"/upload/webHome/template/9bddbbd6-1ad2-4290-b087-caa26bf93197.jpg\",\"showImg_5\":\"/upload/webHome/template/604b7531-384b-4052-93cb-e5d51182023b.jpg\",\"showImg_6\":\"/upload/webHome/template/4673cf82-fa07-4acc-ac7c-e5c7ef9af687.jpg\",\"showImg_7\":\"\",\"showImg_8\":\"\",\"emplate.quickmark\":\"/upload/webHome/template/27b4cb88-b3b2-4046-bedd-c4ad92735310.jpg\",\"emplate.img\":\"/upload/webHome/template/7949e9eb-eb86-4d54-ab09-1271ea202b2e.jpg\"}‘

用的jfinal架構:

 1 /** 2      * 處理錯誤資料 3      */ 4     public void manageSQL(){ 5         String sql="select * from _table"; 6          List<Record> list=Db.find(sql); 7          setAttr("length",list.size()); 8          for(int i=0; i<list.size(); i++){ 9              //取出一條記錄10              Record r=list.get(i);11              //取出json資料12              String json=r.getStr("showImg");13              setAttr("old=",json);14              //去除emplate為key的開頭的錯誤資料15              int ind=json.indexOf(",\"emplate");16              if(ind>0){17                  json=json.substring(0, ind)+"}";18              }19              setAttr("new=",json);20              //去除value為""的元素21              json=json.replaceAll("((?<=\\{)\"\\w+\":\"\",|,*\"\\w+\":\"\")", "");22              setAttr("all:"+i,json);23              24              System.out.println(json+"=========");25              26              //入庫儲存更新27              r.set("showImg", json);28              29              Db.update("home_template",r);30              31          }32          33          renderJson();34     }
4.String對象的一些方法:

length() - 返回長度,注意和js中不同,js裡面是length屬性。

     對應長度:Array的長度-length;List的長度-size(); 

    

java中遇到過的String的一些特性

聯繫我們

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