串匹配:找到子串的前缀和后缀

偶惊奇地发现,我不用作任何"+1", "-1"之类的操作

        String str = "prefixKeywordPostfix";         
        String KEYWORD = "Keyword";
        String prefix = str.substring(0, str.indexOf(KEYWORD));  //"prefix"
        String keyword = str.substring(str.indexOf(KEYWORD),  
str.indexOf(KEYWORD) + KEYWORD.length()); //"Keyword"
        String postfix = str.substring(str.indexOf(KEYWORD)+ KEYWORD.length()); //"Postfix"

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.