我的方法需要检查给定字符串是否等于定义的模板,实际上它不会按预期产生输出。
Inputstring:18122016 with template" 。 "将正确返回" 18.12.2016"
Inputstring:15:00 with template" :"将返回" 15:500"这是错的,预计是15:00
也许某人有比我更清晰的解决方案?
这些是我的测试用例:
@Test
public void testDoCandidateWithTextTemplate() {
ArrayList<String> tWords = new ArrayList<>();
tWords.add(":");
String result = VORecognitionResultsImpl.doCandidateWithTextTemplate("1500", tWords, " :");
assertEquals("15:00", result);
result = VORecognitionResultsImpl.doCandidateWithTextTemplate("15:00", tWords, " :");
assertEquals("15:00", result);
result = VORecognitionResultsImpl.doCandidateWithTextTemplate("18122016", tWords, " . .");
assertEquals("18.12.2016", result);
result = VORecognitionResultsImpl.doCandidateWithTextTemplate("1437", tWords, " ,");
assertEquals("14,37", result);
}
这是方法
public static String doCandidateWithTextTemplate(String tmpTextCandidate,
ArrayList<String> tWords,
String textTemplate)
{
if( textTemplate == null ) return tmpTextCandidate;
if( textTemplate.length() == 0 ) return tmpTextCandidate;
StringBuffer outText = new StringBuffer();
int currentIndex = 0;
boolean isInserted = false;
for(int i = 0; i < textTemplate.length();i++){
currentIndex = i;
if (textTemplate.charAt(i) == ' '){
if (!isInserted){
outText.append(tmpTextCandidate.charAt(i));
}
else{
outText.append(tmpTextCandidate.charAt(i-1));
isInserted = false;
}
}
else{
if (textTemplate.charAt(i) != tmpTextCandidate.charAt(i)){
outText.append(textTemplate.charAt(i));
isInserted = true;
}
}
}
if (currentIndex < tmpTextCandidate.length()-1){
outText.append(tmpTextCandidate.substring(currentIndex-1, tmpTextCandidate.length()));
}
return outText.toString();
}
答案 0 :(得分:3)
您的计数器(currentIndex
)已关闭。一个典型的角落案例问题。
对于1500
- &gt; 15:500
休息:for
条件中断后,i
为3
,但currentIndex
为2
,因为您没有输入循环第四次。因此,当您tmpTextCandidate.substring(currentIndex-1, tmpTextCandidate.length())
tmpTextCandidate.substring(1, 4)
500
outString
时,currentIndex
部分位于分隔符(:)之后。
您的第一个测试用例成功,因为您有两个分隔符计数(。)因此,有时会发生两次错误产生正确的输出,但错误仍然存在。
因此,如果您从tmpTextCandidate
获取字符,则只应增加public static String doCandidateWithTextTemplate(String tmpTextCandidate,
ArrayList<String> tWords,
String textTemplate)
{
if( textTemplate == null ) return tmpTextCandidate;
if( textTemplate.length() == 0 ) return tmpTextCandidate;
StringBuffer outText = new StringBuffer();
int currentIndex = 0;
String tmp = textTemplate;
while(!tmp.isEmpty()) {
while(tmp.indexOf(" ") == 0) {
outText.append(tmpTextCandidate.charAt(currentIndex));
tmp = tmp.substring(1);
currentIndex++;
}
if(!(tmp.charAt(0) == tmpTextCandidate.charAt(currentIndex))) { //This is in case you want both inputs "15:00" as well as the "1500" to output "15:00"
outText.append(tmp.charAt(0));
}
tmp = tmp.substring(1);
}
while(currentIndex < tmpTextCandidate.length()) {
outText.append(tmpTextCandidate.charAt(currentIndex++));
}
return outText.toString();
}
,并确保它不会像现在一样关闭。
编辑:我已经修复了你的功能中的错误,而没有改变方法。
tWords
请注意,这不是最佳解决方案,并且doCandidateWithTextTemplate
参数不会在$('.event').draggable({
opacity: .7,
cursor: 'move',
revert: 'invalid',
});
$('td').droppable({
hoverClass: "hover",
tolerance: "pointer",
drop: function(event, ui) {
if (false === dropped) {
$(this).effect("highlight", {}, 1500);
dropped = true;
$(ui.draggable).detach().css({
top: 0,
left: 0
}).appendTo($(this));
}
},
activate: function(event, ui) {
dropped = false;
}
});
函数的(以及我的)实现中的任何位置使用。