首页 > 试题广场 >

0假设 s1是"Weicome"而 s2 是"welcome

[问答题]
0假设 s1是"Weicome"而 s2 是"welcome", 为下面的陈述编写代码: 
a.检査 s1和 s2 是否相等,然后将结果赋值给一个布尔变量 isEqual。
 b.在忽略大小写的情况下检査 s1和 s2 是否相等,然后将结果賦值给一个布尔变量 isEqual。
 c.比较 s1和 s2, 然后将结果陚值给一个整型变量 X。
 d.在忽略大小写的情况下比较 s1和 s2, 然后将结果陚值给一个整型变量 X。
 e.检査 s1是否有前缀"AAA", 然后将结果陚值给一个布尔变量 b。
 f.检査 s1是否有后缀"AAA", 然后将结果賦值给一个布尔变* b。
 g. 将 s1的长度陚值给一个整型变量 X。
 h.将 s1的第一个字符賦值给一个字符型变量 X。
 i.创建新字符串 S3,它是 s1 和 S2 的组合。
 j.创建 s1的子串,下标从1开始。 
k.创建 s1的子串,下标从1到 4。
 l.创建新字符串 S3, 它将 s1转换为小写。
 m.创建新字符串 S3, 它将 s1转换为大写。
 n.创建新字符串 S3, 它将 s1两端的空白字符去掉。 
o.将 s1中第一次出现的字符 e 的下标陚值给一个 int 型变量 x。
 p.将 s1中最后一次出现的字符串 abc 的下标賦值给一个 int 型变量 x。

1.

import java.util.Scanner;

import java.lang.*;

public class Stringhw {

public static void main(String[] args) {

String s1 = "Welcome";

String s2 = "welcome";

boolean isEqual = s1.equals(s2);

System.out.println(isEqual);

}

}

运行结果:false;


2.

import java.util.Scanner;

import java.lang.*;

public class Stringhw {

public static void main(String[] args) {

String s1 = "Welcome";

String s2 = "welcome";

boolean isEqual = s1.equalsIgnoreCase(s2);

System.out.println(isEqual);

}

}

运行结果:true


3.

import java.util.Scanner;

import java.lang.*;

public class Stringhw {

public static void main(String[] args) {

String s1 = "Welcome";

String s2 = "welcome";

int x = s1.compareTo(s2);

System.out.println(x);

}

}

运行结果:-32


4.

import java.util.Scanner;

import java.lang.*;

public class Stringhw {

public static void main(String[] args) {

String s1 = "Welcome";

String s2 = "welcome";

int x = s1.compareToIgnoreCase(s2);

System.out.println(x);

}

}

运行结果:0


5.

import java.util.Scanner;

import java.lang.*;

public class Stringhw {

public static void main(String[] args) {

String s1 = "Welcome";

String s2 = "welcome";

String s3 = "AAA";

boolean b = s1.startsWith(s3);

System.out.println(b);

}

}

运行结果:false


6.

import java.util.Scanner;

import java.lang.*;

public class Stringhw {

public static void main(String[] args) {

String s1 = "Welcome";

String s2 = "welcome";

String s3 = "AAA";

boolean b = s1.endsWith(s3);

System.out.println(b);

}

}

运行结果:false


7.

import java.util.Scanner;

import java.lang.*;

public class Stringhw {

public static void main(String[] args) {

String s1 = "Welcome";

String s2 = "welcome";

String s3 = "AAA";

int x = s1.length();

System.out.println(x);

}

}

运行结果:7


8.

import java.util.Scanner;

import java.lang.*;

public class Stringhw {

public static void main(String[] args) {

String s1 = "Welcome";

String s2 = "welcome";

String s3 = "AAA";

char x =s1.charAt(0);

System.out.println(x);

}

}

运行结果:W


9.

import java.util.Scanner;

import java.lang.*;

public class Stringhw {

public static void main(String[] args) {

String s1 = "Welcome";

String s2 = "welcome";

String s3 = s1.concat(s2);

System.out.println(s3);

}

}

运行结果:Welcomewelcome


10.

import java.util.Scanner;

import java.lang.*;

public class Stringhw {

public static void main(String[] args) {

String s1 = "Welcome";

String s2 = "welcome";

s1 = s1.substring(1);

System.out.println(s1);

}

}

运行结果:elcome


11.

import java.util.Scanner;

import java.lang.*;

public class Stringhw {

public static void main(String[] args) {

String s1 = "Welcome";

String s2 = "welcome";

s1 = s1.substring(1, 5);

System.out.println(s1);

}

}

运行结果:elco


12.

import java.util.Scanner;

import java.lang.*;

public class Stringhw {

public static void main(String[] args) {

String s1 = "Welcome";

String s2 = "welcome";

String s3 = s1.toLowerCase();

System.out.println(s3);

}

}

运行结果:welcome


13.

import java.util.Scanner;

import java.lang.*;

public class Stringhw {

public static void main(String[] args) {

String s1 = "Welcome";

String s2 = "welcome";

String s3 = s1.trim();

System.out.println(s3);

}

}

运行结果:Welcome


14.

import java.util.Scanner;

import java.lang.*;

public class Stringhw {

public static void main(String[] args) {

String s1 = "Welcome";

String s2 = "welcome";

int x = s1.indexOf('e');

System.out.println(x);

}

}

运行结果:1


15.

import java.util.Scanner;

import java.lang.*;

public class Stringhw {

public static void main(String[] args) {

String s1 = "Welcome";

String s2 = "welcome";

int x = s1.indexOf("abc");

System.out.println(x);

}

}

运行结果:-1

发表于 2022-03-13 15:46:28 回复(0)