集册 Java实例教程 匹配字符串区域

匹配字符串区域

欢马劈雪     最近更新时间:2020-01-02 10:19:05

526
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
匹配字符串区域

public class RegionMatchesDemo {
/**
 * 时 代 J a v a 公 众 号 - N o w J a v  a . c o m 提 供 
**/

    public static void main(String[] args) {

        String searchMe = "Green Eggs and Ham";

        String findMe = "Eggs";

        int searchMeLength = searchMe.length();

        int findMeLength = findMe.length();

        boolean foundIt = false;

        for (int i = 0; i <= (searchMeLength - findMeLength); i++) {

            if (searchMe.regionMatches(i, findMe, 0, findMeLength)) {

                foundIt = true;

                System.out.println(searchMe.substring(i, i + findMeLength));

                break;

            }

        }

        if (!foundIt)

            System.out.println("No match found.");

    }

}


展开阅读全文