在寫代碼的程序中總是會遇到提取字符傳中想要的部分,但每次遇到的情況不一樣,在這里做個總結
1、遇到最多的是ui自動化時,獲取到一個div標簽下的所有文案,但我只需要其中的一部分文案
例如:<div > 9折 </div>,取出數字 9
String Str = "9折";
方法一、將 “折” 替換為空
System.out.println(str.replace("折", ""));
注:像這種又短搭配又簡單的推薦使用replace()
方法二、根據索引來取
System.out.println(str2.substring(0,1));
注:subString(x, y)方法:取指定 開始索引處 到 結束索引處-1 之間的字符;其實用這種方法不穩定,如果是88折,取到的結果就是 8
2、一個動態的字串
例如:這個動態的url,取出其中picture的值
String str = "https://home.cnblogs.com/u/mmikey?picture=1&picture=2&picture=3";
步驟:
第一步:獲取 第一個picture=后面的所有字串
System.out.println(str.replace("https://home.cnblogs.com/u/mmikey?picture=",""));
??1&picture=2&picture=3
第二步:使用split分割出所有數字
String str1 = "1&picture=2&picture=3";
System.out.println(Arrays.toString(str1.split("&picture=")));
??[1, 2, 3]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/499970.html
標籤:其他
上一篇:FileOutputStream(檔案位元組輸出流)
下一篇:day02
