0
点赞
收藏
分享

微信扫一扫

基于SQL语言的数据库管理系统

巧乐兹_d41f 2023-12-05 阅读 11

Java字符串面试题

1. 创建一个字符串变量,并打印输出字符串的内容。

public class Main {
    public static void main(String[] args) {
        String str = "Hello, World!";
        System.out.println(str);
    }
}

2. 如何获取字符串的长度?

public class Main {
    public static void main(String[] args) {
        String str = "Hello, World!";
        int length = str.length();
        System.out.println(length);
    }
}

3. 如何访问字符串中的特定字符?

public class Main {
    public static void main(String[] args) {
        String str = "Hello, World!";
        char ch = str.charAt(0);
        System.out.println(ch); // 输出:H
    }
}

4. 如何在字符串中查找指定的子串并返回位置?

public class Main {
    public static void main(String[] args) {
        String str = "Hello, World!";
        int position = str.indexOf("World");
        System.out.println(position); // 输出:7
    }
}

5. 如何将字符串转换为大写或小写?

public class Main {
    public static void main(String[] args) {
        String str = "Hello, World!";
        String uppercase = str.toUpperCase();
        String lowercase = str.toLowerCase();
        System.out.println(uppercase); // 输出:HELLO, WORLD!
        System.out.println(lowercase); // 输出:hello, world!
    }
}

6. 如何截取字符串的一部分?

public class Main {
    public static void main(String[] args) {
        String str = "Hello, World!";
        String substring = str.substring(0, 5);
        System.out.println(substring); // 输出:Hello
    }
}

7. 如何替换字符串中的指定子串?

public class Main {
    public static void main(String[] args) {
        String str = "Hello, World!";
        String newString = str.replace("World", "Java");
        System.out.println(newString); // 输出:Hello, Java!
    }
}

8. 如何去除字符串两端的空格?

public class Main {
    public static void main(String[] args) {
        String str = "  Hello, World!  ";
        String trimmedString = str.trim();
        System.out.println(trimmedString); // 输出:Hello, World!
    }
}

9. 如何将字符串拆分为数组?

public class Main {
    public static void main(String[] args) {
        String str = "apple,banana,orange";
        String[] array = str.split(",");
        for (String element : array) {
            System.out.println(element);
        }
    }
}

10. 如何将数组的元素连接成一个字符串?

public class Main {
    public static void main(String[] args) {
        String[] array = {"apple", "banana", "orange"};
        String str = String.join(", ", array);
        System.out.println(str); // 输出:apple, banana, orange
    }
}
举报

相关推荐

0 条评论