四,Java字符串

章节目录

标题链接
一,Java概述与Java基础http://t.csdnimg.cn/EBd3W
二,Java程序流程http://t.csdnimg.cn/k6p9l
三,Java数组http://t.csdnimg.cn/ABfr1
四,Java字符串http://t.csdnimg.cn/TrKjL
五,类和对象基础http://t.csdnimg.cn/6HRem
六,构造方法http://t.csdnimg.cn/M4gTk
七,this关键字和static关键字http://t.csdnimg.cn/wzVJs
八,类的继承http://t.csdnimg.cn/WOKIh
九,实践课堂练习http://t.csdnimg.cn/k9iix
十,封装课堂练习http://t.csdnimg.cn/oYl68
十一,多态http://t.csdnimg.cn/gFhx8
十二,抽象类http://t.csdnimg.cn/pYKtx
十三,实现接口http://t.csdnimg.cn/vXK3v

Java字符串

一. 简答题(共2题,100分)

1. (简答题)给定字符串String str1 = “hello”,字符串String str2 = “world”;1. 判断两个字符串的长度是否相等,输出结果;2.分别输出两个字符串字母‘o’的位置;3. 将两个字符串进行拼接并输出;4.将拼接的字符串“hello”后加上空格并输出。5.将拼接的整个字符串进行逆序,并输出。提交程序和运行截图,需注意编码规范。

public class Day1031 {
 
    public static void tixtl1() {
        String str1 = "hello";
        String str2 = "world";
        //1. 判断两个字符串的长度是否相等,输出结果;
        boolean isLength = str1.length() == str2.length();
        System.out.println("两个字符串的长度是否相等:" + isLength);          //true
 
        //2.分别输出两个字符串字母‘o'的位置;
        int index1 = str1.indexOf('o');
        int index2 = str2.indexOf('o');
        System.out.println("字符串 str1 中字母 'o' 的位置:" + index1);         //4
        System.out.println("字符串 str2 中字母 'o' 的位置:" + index2);         //1
 
        //3. 将两个字符串进行拼接并输出;
        String concatStr = str1.concat(str2);
        System.out.println("拼接后的字符串:" + concatStr);           //helloworld
 
        //4.将拼接的字符串“hello”后加上空格并输出。
        String string = str1 + " ";
        String spacedStr = string.concat(str2);
        System.out.println("加上空格后的字符串:" + spacedStr);             //hello world
 
        //5.将拼接的整个字符串进行逆序,并输出。
        StringBuilder reversedStr = new StringBuilder(concatStr);
        reversedStr.reverse();
        System.out.println("逆序后的字符串:" + reversedStr.toString());        //dlrowolleh
    }
 
    public static void main(String[] args) {
        Day1031.tixtl1();
    }
}

在这里插入图片描述

2. (简答题)给定字符串String str = “A man, a plan, a canal: Panama”。如果在将所有大写字符转换为小写字符、并移除所有非字母数字字符之后,字符串正着读和反着读都一样,则可以认为该字符串是一个 回文串 。判断给定的字符串是否为回文串。提交程序和运行截图,需注意编码规范。

public class tixtl2 {
    public static void main(String[] args) {
        String str = "A man, a plan, a canal: Panama";
 
        // 将所有大写字符转换为小写字符,并移除所有非字母数字字符
        String filteredStr = str.toLowerCase().replaceAll("[^a-z0-9]", "");
 
        // 判断字符串正着读和反着读是否一样
        boolean isPalindrome = isPalindrome(filteredStr);
 
        // 输出结果
        if (isPalindrome) {
            System.out.println("给定的字符串是回文串");
        } else {
            System.out.println("给定的字符串不是回文串");
        }
    }
 
    private static boolean isPalindrome(String str) {
        int left = 0;
        int right = str.length() - 1;
 
        while (left < right) {
            if (str.charAt(left) != str.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
 
        return true;
 
    }
 
}

在这里插入图片描述

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值