我尝试将UTF8字符串转换为Java Unicode字符串.
String question = request.getParameter("searchWord");
byte[] bytes = question.getBytes();
question = new String(bytes, "UTF-8");
Run Code Online (Sandbox Code Playgroud)
输入是中文字符,当我比较每个字符的十六进制代码时,它是相同的中文字符.所以我很确定charset是UTF8.
我哪里出错了?
我正在制作一个正则表达式来查找文本中句子的结尾.在这里,我假设任何句子都可以以.!?有时虽然有人喜欢两个写!!!!!! 在他们和他们的句子.所以我想替换任何重复的点,感叹号或问号.但我想允许使用'...'.我怎么能包含这个例外?请指教,谢谢!
Pattern p = null;
try {
//([!?.] with optional spaces), followed by ([!?.] with optional spaces) repeated 1 or more times
p = Pattern.compile("([!?.]\\s*)([!?.]\\s*)+");
}
catch (PatternSyntaxException pex) {
pex.printStackTrace();
System.exit(0);
}
//get the matcher
Matcher m = p.matcher(this.sentence);
int index = 0;
while(m.find(index))
{
System.out.println(this.sentence);
System.out.println(p.toString());
String toReplace = sentence.substring(m.start(), m.end());
toReplace = toReplace.replaceAll("\\.","\\\\.");
toReplace =toReplace.replaceAll("\\?","\\\\?");
String replacement = ""+sentence.charAt(m.start());
this.sentence = this.sentence.replaceAll(toReplace, replacement);
System.out.println("");
index = m.end();
System.out.println(this.sentence);
}
Run Code Online (Sandbox Code Playgroud) 我有这样的结构:
源代码 -|com -|公司 -|核心--> JAVA类 -|规则 --> 文本文件
Core 内部是一个 Java 类,它使用:
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚要使用什么相对路径。现在我用:
“..\\..\\..\\..\\rules\\rule1.txt”
我也尝试过:
“../../../../rules/rule1.txt”
我哪里做错了?
我从我的HTML页面获取一个字符串到我的Java HTTPServlet.根据我的要求,我得到显示中文字符的ASCII码:
"可以告诉我" (没有空格)
如何将此字符串转换为Unicode?
HTML代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Find information</title>
<link rel="stylesheet" type="text/css" href="layout.css">
</head>
<body>
<form id="lookupform" name="lookupform" action="LookupServlet" method="post" accept-charset="UTF-8">
<table id="lookuptable" align="center">
<tr>
<label>Question:</label>
<td><textarea cols="30" rows="2" name="lookupstring" id="lookupstring"></textarea></td>
</tr>
</table>
<input type="submit" name="Look up" id="lookup" value="Look up"/>
</form>
Run Code Online (Sandbox Code Playgroud)
Java代码:
request.setCharacterEncoding("UTF-8");
javax.servlet.http.HttpSession session = request.getSession();
LoginResult lr = (LoginResult) session.getAttribute("loginResult");
String[] question = request.getParameterValues("lookupstring");
Run Code Online (Sandbox Code Playgroud)
如果我打印问题[0],那么我得到这个值:"可以告诉我"