使用前端传来的文字进行精确匹配
/**
* 使用前端传来的文字进行精确匹配,替换指定字符
*/
@GetMapping("/findByWords")
public void selectOneByText(HttpServletResponse response,String text) throws IOException{
//替换搜索内容中制定的字符串
if(text != null && !"".equals(text)){
final String s2 = "?";
final String s1 = ",";
if(text.contains(s2)){
text = text.replace(s2, "");
}else if(text.contains(s1)){
text = text.replace(s1, "");
}
SxVideoRecording recording = new SxVideoRecording();
recording.setVName(text);
List<SxVideoRecording> sxVideoRecordings = sxVideoRecordingService.selectSxVideoRecordingList(recording);
if(sxVideoRecordings.size() > 0){
SxVideoRecording recording1 = sxVideoRecordings.get(0);
writeJSON(recording1, response, "200", "查询成功");
}
writeJSON(null, response, "201", "暂无此课程");
}
writeJSON(null, response, "201", "请输入您要搜索得内容");
}
PS替换源码
/**
* Replaces each substring of this string that matches the literal target
* sequence with the specified literal replacement sequence. The
* replacement proceeds from the beginning of the string to the end, for
* example, replacing "aa" with "b" in the string "aaa" will result in
* "ba" rather than "ab".
*
* @param target The sequence of char values to be replaced
* @param replacement The replacement sequence of char values
* @return The resulting string
* @since 1.5
*/
public String replace(CharSequence target, CharSequence replacement) {
return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}