String str1 = "java判断是否为汉字"
String str2 = "全为汉字"
String reg = "[\\u4e00-\\u9fa5]+"
boolean result1 = str1.matches(reg)//false
boolean result2 = str2.matches(reg)//true
2.提取字符串中的汉字。
String str = "java怎么把asdasd字符串中的asdasd的汉字取出来"
String reg = "[^\u4e00-\u9fa5]"
str = str.replaceAll(reg, " ")
System.out.println(str)
3.判断字符串中是否含有汉字。
boolean result = (str.length() == str.getBytes().length)//true:无汉字 false:有汉字
4.获取字符串中汉字的个数。
int count = 0
String reg = "[\\u4e00-\\u9fa5]"
String str = "java获取汉字Chinese的个数"
Pattern p = Pattern.compile(reg)
Matcher m = p.matcher(str)
while (m.find()) {for (int i = 0i <= m.groupCount()i++) {count = count + 1}}
System.out.println("共有汉字 " + count + "个 ")
package testimport java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.net.Authenticator
import java.net.HttpURLConnection
import java.net.PasswordAuthentication
import java.net.URL
import java.net.URLConnection
import java.util.Properties
public class URLTest {
// 一个public方法,返回字符串,错误则返回"error open url"
public static String getContent(String strUrl) {
try {
URL url = new URL(strUrl)
BufferedReader br = new BufferedReader(new InputStreamReader(url
.openStream()))
String s = ""
StringBuffer sb = new StringBuffer("")
while ((s = br.readLine()) != null) {
sb.append(s + "/r/n")
}
br.close()
return sb.toString()
} catch (Exception e) {
return "error open url:" + strUrl
}
}
public static void initProxy(String host, int port, final String username,
final String password) {
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,
new String(password).toCharArray())
}
})
System.setProperty("http.proxyType", "4")
System.setProperty("http.proxyPort", Integer.toString(port))
System.setProperty("http.proxyHost", host)
System.setProperty("http.proxySet", "true")
}
public static void main(String[] args) throws IOException {
String url = "https://www.jb51.net"
String proxy = "http://192.168.22.81"
int port = 80
String username = "username"
String password = "password"
String curLine = ""
String content = ""
URL server = new URL(url)
initProxy(proxy, port, username, password)
HttpURLConnection connection = (HttpURLConnection) server
.openConnection()
connection.connect()
InputStream is = connection.getInputStream()
BufferedReader reader = new BufferedReader(new
InputStreamReader(is))
while ((curLine = reader.readLine()) != null) {
content = content + curLine+ "/r/n"
}
System.out.println("content= " + content)
is.close()
System.out.println(getContent(url))
}
}
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.Arrays
public class NumberSplit {
public StringBuffer getString(String str) {
StringBuffer strbuf = new StringBuffer("t")
boolean flag = true
for (int i = 0i <str.length()i++) {
char c = str.charAt(i)
// 判断是否是数字
if (c >= '0'&&c <= '9') {
// 判断和字符串中的数字是否重复
for (int j = 1j <strbuf.length()j++) {
if (c == strbuf.charAt(j)) {
// 如果重复,标志位数值为false,并跳出循环否则标志位设置为true.
flag = false
break
} else
flag = true
}
// 只有在即是数字又不重复的情况下才将改字符拼接到字符串上.
if (flag) {
strbuf.append(c)
}
}
}
return strbuf
}
public static void main(String args[]) throws Exception {
System.out.println("请输入一段字符串,并以回车结束")
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in))
String str = buf.readLine()
StringBuffer strbuf = new NumberSplit().getString(str)
// 将返回的StringBuffer转换为字符数组
char c[] = strbuf.deleteCharAt(0).toString().toCharArray()
// 对字符数组排序
Arrays.sort(c)
// 输出字符数组
for (int i = 0i <strbuf.length()i++) {
System.out.print(c[i])
}
}
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.Arrays
public class NumberSplit {
public StringBuffer getString(String str) {
StringBuffer strbuf = new StringBuffer("t")
boolean flag = true
for (int i = 0i <str.length()i++) {
char c = str.charAt(i)
// 判断是否是数字
if (c >= '0'&&c <= '9') {
// 判断和字符串中的数字是否重复
for (int j = 1j <strbuf.length()j++) {
if (c == strbuf.charAt(j)) {
// 如果重复,标志位数值为false,并跳出循环否则标志位设置为true.
flag = false
break
} else
flag = true
}
// 只有在即是数字又不重复的情况下才将改字符拼接到字符串上.
if (flag) {
strbuf.append(c)
}
}
}
return strbuf
}
public static void main(String args[]) throws Exception {
System.out.println("请输入一段字符串,并以回车结束")
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in))
String str = buf.readLine()
StringBuffer strbuf = new NumberSplit().getString(str)
// 将返回的StringBuffer转换为字符数组
char c[] = strbuf.deleteCharAt(0).toString().toCharArray()
// 对字符数组排序
Arrays.sort(c)
// 输出字符数组
for (int i = 0i <strbuf.length()i++) {
System.out.print(c[i])
}
}