我有这部分代码,我无法正确理解。我知道它会根据数据库中的值构建一个字符串。我认为这只是title
:
private String createSearchFieldContent(Report report) {
return createSearchFieldContent(report.getTitle(), report.getDescription());
}
private String createSearchFieldContent(OverrideableStringValue title,
OverrideableStringValue description) {
StringBuilder builder = new StringBuilder(getValue(title));
if (StringUtils.isNotBlank(getValue(description))) {
builder.append(" ").append(getValue(description));
}
String searchTerm = StringUtils.replaceAll(builder.toString(), "\n", " ");
return unaccent(searchTerm);
}
我还有另一个要添加到searchTerm
中的值。它存储在report.getOwner()
中。
searchTerm
,title
和description
的新owner
?
谢谢!
答案 0 :(得分:1)
尝试以下代码:
private String createSearchFieldContent(Report report) {
return createSearchFieldContent(report.getTitle(), report.getDescription(), report.getOwner());
}
private String createSearchFieldContent(OverrideableStringValue title,
OverrideableStringValue description, OverrideableStringValue owner) {
StringBuilder builder = new StringBuilder(getValue(title));
if (StringUtils.isNotBlank(getValue(description))) {
builder.append(" ").append(getValue(description));
}
if (StringUtils.isNotBlank(getValue(owner))) {
builder.append(" ").append(getValue(owner));
}
String searchTerm = StringUtils.replaceAll(builder.toString(), "\n", " ");
return unaccent(searchTerm);
}
在上面的代码中,我们像其他两个方法一样将owner属性传递给createSearchFieldContent
方法,然后将owner属性像描述一样附加到字符串生成器对象。
答案 1 :(得分:0)
您可以将新数据分配给searchTerm:
searchTerm = StringUtils.replaceAll(builder.toString(), "\n", " ")
成为
String part2 = "something you want to add";
searchTerm = searchTerm + part2;
或者您可以使用StringBuilder将新数据追加到
builder.append("new data")
builder.append("newer data")
这将添加到StringBuilder中,该字符串将包含在searchTerm的字符串值中。