Java程序编写 在类TestString的主方法中创建String类对象s(Hello

2025-04-12 02:31:57
推荐回答(2个)
回答(1):

    String s = new String("Hello World!");
    //  String s = "Hello World!";     两种方式
        System.out.println("s的长度为:"+s.length());
        System.out.println("s的第二个字符为:"+s.charAt(2));//输出的为l,参数是从0开始的
        System.out.println("s小写转大写:"+s.toUpperCase());
        s = s+" How are you?";
        System.out.println("连接后字符:"+s);

希望采纳!

回答(2):

public class TestString {
//主方法
public static void main(String[] args) {
//String s = "Hello World!"; 
String s = new String("Hello World!");//String类对象s
System.out.println("s的长度为:"+s.length());
System.out.println("第二个字符为:"+s.substring(1, 2));
//System.out.println("第二个字符为:"+s.charAt(1));//第二种方法
System.out.println("转化为大写"+s.toUpperCase());
System.out.println(s.concat(" How are you?"));//本题应该是考察这个函数
//System.out.println(s+" How are you?"));//第二种方法
}
}