Java:在字符串中查找字符串

时间:2014-01-15 17:23:06

标签: java string search find

我正在尝试制作某种能够检测java语法并突出显示该代码的系统。但是,我似乎无法在字符串中找到字符串。

这是我到目前为止所做的:

import java.util.Scanner;

public class Client {

    private static String[] javaKeywords = {
        "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue",
        "default", "do", "double", "else", "enum", "extends", "final", "finnaly", "float", "for", "goto", "if",
        "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "primitve",
        "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", 
        "this", "throw", "throws", "transient", "try", "void", "volatile", "while"
    };

    private static String[] javaSyntax = {
        "++", "--", "~", "!", "*", "/", "%", "+", "-", " <<", ">>", ">>>", "<", ">", "<=", ">=", "==", "!=", "&",
        "^", "|", "&&", "||", "?", ":", "=", "+=", "-=", "/=", "%=", "&=", "^=", "|=", "<<=", ">>=", ">>>="
    };

    private static Scanner scanner = new Scanner(System.in);
    private static StringBuilder builder = new StringBuilder();

    public static void main(String args[]) {

        String input;

        while(!(input = scanner.nextLine()).toLowerCase().contains("exit")) {

            switch(input.toLowerCase()) {
                case "print":
                    System.out.println(builder.toString());
                    continue;

                case "clear":
                    builder = new StringBuilder();
                    continue;
            }

            builder.append(input + "\n");
            codeWrap(builder.toString());

        }

   }

    private static void codeWrap(String code) {
        int A4I = 0, position; // A4I = Account for insert length
        for(String keyword: javaKeywords) {

            if((position = findInString(code, keyword)) != -1) {
                builder.insert(position + A4I, "[code]");
                A4I += 6;
                builder.insert(position + keyword.length() + A4I, "[/code]");
                A4I += 7;
           }

        }
    }

    private static int findInString(String string, String keyword) {
        for(int index = 0, keywordIndex = 0; index < string.length(); index++) {

            keywordIndex = (string.charAt(index) == keyword.charAt(keywordIndex)) ? ++keywordIndex : 0;

            if(keywordIndex == keyword.length()) return ((index + 1) - keyword.length());

        }
        return -1;
    }

}

这在很大程度上起作用,但是如果你尝试用两个关键词包装一个句子, 如果关键字b在javaKeywords数组中位于关键字a之前,则会返回奇怪的结果。

例如: b(while)之前的(摘要)结果

abstract while
print
[code]abstract[/code] [code]while[/code]

在(抽象)

之前用b(while)得到的结果
while abstract
print
while [code]a[code]bstra[/code]ct[/code]

1 个答案:

答案 0 :(得分:1)

在这种情况下,您的A4I变量不需要,并且会导致您进行已计算的偏移量。请注意以下事项:

while abstract
>loop finds 'abstract' at position 6
while [code]abstract[/code]
>A4I is now making all offsets +13
>loop finds 'while' found at position 0
>you add that +13 offset to the insert making it drop right in the middle of the abstract

您还将2个字符串传递给codeWrap方法,因为字符串是可以在您搜索字符串中的索引然后在不同的字符串上使用它。您可以在程序中找到一些其他奇怪的问题,但这应该可以解决您的问题

private static void codeWrap() {
    int position;
    for(String keyword: javaKeywords) {

        if((position = findInString(builder.toString(), keyword)) != -1) {
            builder.insert(position, "[code]");
            builder.insert(position + keyword.length()+6, "[/code]");
       }

    }
}