日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 获取url中的参数,验证邮箱格式,截取字符串中键值对的值,String的字节长度,去空格,替换字符

發布時間:2023/12/10 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 获取url中的参数,验证邮箱格式,截取字符串中键值对的值,String的字节长度,去空格,替换字符 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
String ss="hello"; byte[] buff=ss.getBytes(); int f=buff.length; System.out.println(f);

?字節長度。一個中文是3。其他是1

?

1、獲取url中的參數

創建string?

?

String urls= "http://www.yxtribe.com/yuanxinbuluo/weixin/getJsp?url=wechatweb/business-style-five&param=330&appFlg=1"; String param= URLRequest(urls).get("param");//是330

實現方法

/*** 解析出url參數中的鍵值對* 如 "index.jsp?Action=del&id=123",解析出Action:del,id:123存入map中* @param URL url地址* @return url請求參數部分*/ public static Map<String, String> URLRequest(String URL) {Map<String, String> mapRequest = new HashMap<String, String>();String[] arrSplit=null;String strUrlParam=TruncateUrlPage(URL);if(strUrlParam==null){return mapRequest;}//每個鍵值為一組 www.2cto.comarrSplit=strUrlParam.split("[&]");for(String strSplit:arrSplit){String[] arrSplitEqual=null;arrSplitEqual= strSplit.split("[=]");//解析出鍵值if(arrSplitEqual.length>1){//正確解析mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);}else{if(arrSplitEqual[0]!=""){//只有參數沒有值,不加入mapRequest.put(arrSplitEqual[0], "");}}}return mapRequest; }/*** 去掉url中的路徑,留下請求參數部分* @param strURL url地址* @return url請求參數部分*/ private static String TruncateUrlPage(String strURL) {String strAllParam=null;String[] arrSplit=null;strURL=strURL.trim();arrSplit=strURL.split("[?]");if(strURL.length()>1){if(arrSplit.length>1){if(arrSplit[1]!=null){strAllParam=arrSplit[1];}}}return strAllParam; }

?

?2、驗證郵箱格式,電話格式,密碼格式

??public static boolean isMobileNO(String mobiles) {
????????String telRegex = "13\\d{9}|14[57]\\d{8}|15[012356789]\\d{8}|18[012356789]\\d{8}|17[01678]\\d{8}";
????????if (TextUtils.isEmpty(mobiles)) return false;
????????else return mobiles.matches(telRegex);
????}
?
????public static boolean passWordVerify(String pass) {
????????Pattern p = Pattern.compile("^[A-Za-z0-9]{6,12}$");
????????//Pattern p = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z_]{6,12}$");
????????return p.matcher(pass).matches();
????}
?
????public static boolean mailAddressVerify(String mailAddress) {
????????String emailExp = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
????????Pattern p = Pattern.compile(emailExp);
????????return p.matcher(mailAddress).matches();
????}


3、截取字符串中鍵值對的值

String data ="Address[addressLines=[0:\"廣東省東莞市健升大廈\"],feature=健升大廈,admin=廣東省,sub-admin=null,locality=東莞市,thoroughfare=null,postalCode=null,countryCode=CN,countryName=中國,hasLatitude=true, latitude=23.025354,hasLongitude=true,longitude=113.748738,phone=null,url=null,extras=Bundle[mParcelledData.dataSize=92]]"; int startCity = data.indexOf("locality=") + "locality=".length(); int endCity = data.indexOf(",", startCity); String city = data.substring(startCity, endCity);//獲取市 int startPlace = data.indexOf("feature=") + "feature=".length(); int endplace = data.indexOf(",", startPlace); String place = data.substring(startPlace, endplace);//獲取地址 LgqLogutil.e(city+place);

04-24 14:53:27.140 20566-20566/com.tianxin.ttttest E/lgq: onCreate----東莞市健升大廈

?

4、去空格

??s = s.replaceAll("\r|\n", "");

?

5、替換字符串

String abtest = "123abc";
String result ="";
result = abtest.replace("ab","hhhh");//是"123hhhhc"
?

6、根據游標截取字符串

? ? ? ? String abtest = "123abc";
? ? ? ? String result ="";
// ? ? ? ?result = abtest.substring(3);//="abc
? ? ? ? result = abtest.substring(3,4);//="a

7、去空格,替換字符

  • String str = " hell , 午飯,,晚飯 ";

  • String str2 = str.replaceAll(" ", "");

  • Pattern pattern = Pattern.compile("\t|\r|\n|\\s*|\\�"); Matcher matcher = pattern.matcher(src); dest = matcher.replaceAll("");

    ?

    8、bundleToString

    private static String bundleToString(Bundle bundle) {if (bundle == null) {return "null";}final StringBuilder sb = new StringBuilder();for (String key: bundle.keySet()) {sb.append(key).append("=>").append(bundle.get(key)).append("\n");}return sb.toString(); }

    總結

    以上是生活随笔為你收集整理的android 获取url中的参数,验证邮箱格式,截取字符串中键值对的值,String的字节长度,去空格,替换字符的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。