String IPAddress = ""; InetAddress ReturnStr1 = null; try { AppLog.Loge("完整域名的推送地址为:"+Constant.DEVICE_PUSH_SERVER); URL url=new URL(Constant.DEVICE_PUSH_SERVER); String hostStr=url.getHost(); int hostPort=url.getPort(); LgqLogPlus.e("域名的端口,Port为0000:"+hostStr); ReturnStr1 = java.net.InetAddress.getByName(hostStr); LgqLogPlus.e("域名的端口,Port为1111:"+ReturnStr1); IPAddress = ReturnStr1.getHostAddress(); LgqLogPlus.e("根据域名得到IP为22222:"+IPAddress); UserComm.saveDognessConfig(mContext,IPAddress,hostPort); } catch (Exception e) { AppLog.Loge("域名获取IP地址,获取失败"); UserComm.saveDognessConfig(mContext,"",80);//如果获取失败情况下 e.printStackTrace(); }
public static boolean isPwd(String str){ boolean isDigit = false;//定义一个boolean值,用来表示是否包含数字 boolean isLowerCase = false;//定义一个boolean值,用来表示是否包含字母 boolean isUpperCase = false; for (int i = 0; i < str.length(); i++) { if (Character.isDigit(str.charAt(i))) { isDigit = true; } else if (Character.isLowerCase(str.charAt(i))) { isLowerCase = true; } else if (Character.isUpperCase(str.charAt(i))) { isUpperCase = true; } } String regex = "^[a-zA-Z0-9]+$"; boolean isRight = isDigit && isLowerCase && isUpperCase && str.matches(regex); return isRight; }
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¶m=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.com
arrSplit=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;
}
//处理二维码数据返回 public static String dealWithQrStr(String qrStr) { String useStr = qrStr;//处理后,有用的字符串 try { if(qrStr.startsWith("http://") || qrStr.startsWith("https://")){ Uri uri = Uri.parse(qrStr); String hostStr = uri.getHost(); String device_mac = uri.getQueryParameter("mac"); String device_code = uri.getQueryParameter("id"); String device_imei = uri.getQueryParameter("imei"); if (!IsNullString(device_mac) && !device_mac.equalsIgnoreCase("null")){ if(!device_mac.contains(":")){ StringBuilder builder=new StringBuilder(device_mac); for (int i = 0; i <builder.length() ; i++) { if(i%3==0){ //对mac地址插入:号 builder.insert(i,":"); } } builder.delete(0,1); useStr = builder.toString(); }else{ useStr = device_mac; } }else if(!IsNullString(device_code) && !device_code.equals("null")){ useStr = device_code; }else if(!IsNullString(device_imei) && !device_imei.equals("null")){ useStr = device_imei; } } } catch (Exception e) { e.printStackTrace(); } return useStr; } //判断字符是否为空或者没数据 public static boolean IsNullString(String str) { if (str != null && !TextUtils.isEmpty(str) && !TextUtils.equals("", str.trim())) { return false; } else { return true; } }
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();
}
public static boolean isEmail(String email) { String str = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$"; Pattern p = Pattern.compile(str); Matcher m = p.matcher(email); return m.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();
}