判断字符串的开头和结尾

package lianxi;

import java.util.*;

public class kaishihejiewei {

    public static void main(String[] args) {
        System.out.print("请输入字符:");
        Scanner str = new Scanner(System.in);
        String a = str.next();
        String b = "abcdefghijklmn";
        if (a.length() > b.length()) {
            System.out.println("输入的字符长度有误");

        } else {
            if (b.indexOf(a) == 0) {
                System.out.println("输入的字符是字符串的开头");

            } else {
                System.out.println("输入的字符不是字符串的开头");
            }
            if (a.equals(b.substring(b.length() - a.length()))) {
                System.out.println("输入的字符是字符串的结尾");
            } else {
                System.out.println("输入的字符不是字符串的结尾");
            }

        }
    }

}

posted @ 2016-05-13 21:12  凌零聆  阅读(523)  评论(0编辑  收藏  举报