判断字符串的开头和结尾

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("输入的字符不是字符串的结尾");
            }

        }
    }

}

原文地址:https://www.cnblogs.com/wenwen123/p/5491123.html