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

Java:在字符串中查找字符串,java,string,search,find,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", "c

我正在尝试制作某种系统来检测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;
    }

}
a(摘要)前带b(while)的结果


在这种情况下,不需要使用
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]");
       }

    }
}

我建议使用yacc和flex来检查文本是否是Java代码(或者至少类似于Java代码)?谢谢!这看起来像是我最后可能会用到的东西。啊,谢谢!我一直在兜圈子,试图找出问题所在。记住,如果你有问题,只需拿出一些println来调试你的程序。我刚刚复制了你的两个方法,在你的主方法中放了两行调试代码:
builder.append(“abstract while”);代码包装()。此外,如果问题已解决,请标记为接受答案:)
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
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]");
       }

    }
}