首先,回文是指类似于“12345”,“abcdcba”的形式,即正念和反念都是一样的字符串
判断字符串是否是回文,这边介绍2种办法
1、将字符串翻转,判断翻转后的字符串和原字符串是否相等
public static void main(String[] args) { String s="abcdcba"; // 用StringBuilder的reverse方法将字符串反转 StringBuilder sb=new StringBuilder(s); String afterReverse=sb.reverse().toString(); //判断反转后的字符串与原字符串是否相等,可用compareTo,equals, int isequal=afterReverse.compareTo(s); //若相等则输出0 if (isequal==0){ System.out.println("是回文"); }else System.out.println("不是回文"); }
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。