首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模板化输入字符串

模板化输入字符串
EN

Stack Overflow用户
提问于 2017-04-25 19:05:48
回答 1查看 61关注 0票数 0

我的方法需要检查给定的字符串是否等于定义的模板,实际上它不会产生预期的输出。

输入字符串: 18122016,模板为“.”。将正确返回"18.12.2016“

Inputstring: 15:00使用模板“:”将返回"15:500“,这是错误的,预期为15:00

也许有人有比我更清晰的解决方案?

以下是我的测试用例:

代码语言:javascript
复制
@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);      

}

下面是方法

代码语言:javascript
复制
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();

}
EN

回答 1

Stack Overflow用户

发布于 2017-04-25 19:22:25

你的计数器(currentIndex)少了一个。一个典型的角落案例问题。

对于1500 -> 15:500 break:在for条件中断之后,i3,但currentIndex2,因为您第四次没有进入循环。因此,当您执行tmpTextCandidate.substring(currentIndex-1, tmpTextCandidate.length())时,它是tmpTextCandidate.substring(1, 4),对于分隔符(:)之后的那部分outString,它是500

您的第一个测试用例成功了,因为您有两个分隔符(.)因此,就像有时会发生的那样,两个错误会产生正确的输出,但错误仍然存在。

因此,如果您从tmpTextCandidate中获取字符,并确保它不是像现在这样减一,那么您应该只递增currentIndex

编辑:我已经修复了你的函数中的错误,没有改变方法。

代码语言:javascript
复制
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();
}

请注意,这不是最佳解决方案,而且在您的(以及我的) doCandidateWithTextTemplate函数实现中的任何地方都不会使用tWords参数。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43608947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档