Java 常用字串操作總結

2021-09-09 01:24:42 字數 1937 閱讀 5466

1. string轉ascii碼

public static string stringtoascii(string value)  else 

}return sbu.tostring();

}

2. ascii碼轉string

public static string asciitostring(string value) 

stringbuffer sbuffer = new stringbuffer();

char chars = value.tochararray();

for (int i = 0; i < chars.length; i = i + 2)

}return sbuffer.tostring();

}

3. 輸入流轉位元組陣列

public static string inputstream2byte(inputstream inputstream) throws ioexception 

bos.close();

return new string(bos.tobytearray(), "utf-8");

}

4. 輸入流轉字串

public static string inputstreamtostring(inputstream in) throws exception

5. 判斷輸入是不是包含中文

public static boolean ischinese(char c) 

return false;

}public static boolean ischinese(string strname)

}return false;

}

6. 檢驗子網掩碼的合法性:子網掩碼轉化為二進位制之後不能全是1,也不能全是0,必須是連續的1或者連續的0,也就是說1和0不能交替出現。

實現方式,先將4個整數字段按照每八位拼接起來,軟後轉化為二進位制顯示方式,使用正規表示式進行匹配。

public static boolean checkmask(string maskstr)  

binaryval += binarystr;

} pattern regxpattern = pattern.compile("^[1]*[0]*$");

if(regxpattern.matcher(binaryval).matches())

else

}

7. 檢查ip的合法性,並轉化為整數表示

public static boolean validip(string ip) 

string ip_regex = "\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\."

+ "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\."

+ "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\."

+ "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b";

pattern ippattern = pattern.compile(ip_regex);

matcher matcher = ippattern.matcher(ip);

return matcher.matches();

}public static integer ipstring2int(string strip)

mysql常用字串 MYSQL常用字串函式寶典

mysql常用字串函式 想更進一步加強自己在查詢語句方面的能力,需要掌握常用函式。字串函式 1 concat s1,s2,sn 將s1,s2,sn串聯成乙個字串。exp root test 14 43 desc t1 field type null key default extra id int ...

php常用字串總結

strlen 功能是輸出字串的長度 str hello world result strlen str echo result 輸出字串長度 11 str2 你好 世界 result2 mb strlen str2,utf8 中文 echo result2 輸出字串長度 5 trim 去除字串首尾處...

C 常用字串操作

獲得漢字的區位碼 byte array new byte 2 array system.text.encoding.default.getbytes 啊 int i1 short array 0 0 int i2 short array 1 0 unicode解碼方式下的漢字碼 array syst...