早教吧作业答案频道 -->其他-->
使用Java的String类操作字符串和子串。(1)声明一个String对象,其内容为“Iamastudent,Iamatccit”;(2)输出字符串的长度;(3)输出字符串的第一个和最后一个字符;(4)输出字符串的
题目详情
使用Java的String类操作字符串和子串。
(1)声明一个String对象,其内容为“I am a student,I am at ccit”;
(2)输出字符串的长度;
(3)输出字符串的第一个和最后一个字符;
(4)输出字符串的第一个和最后一个单词;
(5)将其中的‘c’替换为‘C’输出;
(6)输出其中的各个单词;
(7)判断字符串中是否包含“ma”这个单词;
(8)判断字符串是否以“I”开头,以“I”结尾。
编写JAVA程序:
(1)声明一个String对象,其内容为“I am a student,I am at ccit”;
(2)输出字符串的长度;
(3)输出字符串的第一个和最后一个字符;
(4)输出字符串的第一个和最后一个单词;
(5)将其中的‘c’替换为‘C’输出;
(6)输出其中的各个单词;
(7)判断字符串中是否包含“ma”这个单词;
(8)判断字符串是否以“I”开头,以“I”结尾。
编写JAVA程序:
▼优质解答
答案和解析
public class Du02 {
public static void main(String[] args) {
String str = "I am a student,I am at ccit";
System.out.println(str.length());
System.out.println(str.charAt(0));
System.out.println(str.charAt(str.length()-1));//
int firstIndex = str.indexOf(" ");
System.out.println("First word: " + str.substring(0, firstIndex));
int lastIndex = str.lastIndexOf(" ");
System.out.println("Last word: " + str.substring(lastIndex));
System.out.println(str.replace('c', 'C')); //
String[] ary = str.replace(",", " ").split("\\s+");
for(int i =0; i < ary.length; i++){
if(!ary[i].trim().equals("")){
System.out.println(ary[i].trim());
}
}
System.out.println("String contains ma? " + str.concat(" ma "));
System.out.println("String starts with 'i', ends with 'i'? " + str.matches("I(.)*I"));
}
}
-----------testing
27
I
t
First word: I
Last word: ccit
I am a student,I am at CCit
I
am
a
student
I
am
at
ccit
String contains ma? I am a student,I am at ccit ma
String starts with 'i', ends with 'i'? false
public static void main(String[] args) {
String str = "I am a student,I am at ccit";
System.out.println(str.length());
System.out.println(str.charAt(0));
System.out.println(str.charAt(str.length()-1));//
int firstIndex = str.indexOf(" ");
System.out.println("First word: " + str.substring(0, firstIndex));
int lastIndex = str.lastIndexOf(" ");
System.out.println("Last word: " + str.substring(lastIndex));
System.out.println(str.replace('c', 'C')); //
String[] ary = str.replace(",", " ").split("\\s+");
for(int i =0; i < ary.length; i++){
if(!ary[i].trim().equals("")){
System.out.println(ary[i].trim());
}
}
System.out.println("String contains ma? " + str.concat(" ma "));
System.out.println("String starts with 'i', ends with 'i'? " + str.matches("I(.)*I"));
}
}
-----------testing
27
I
t
First word: I
Last word: ccit
I am a student,I am at CCit
I
am
a
student
I
am
at
ccit
String contains ma? I am a student,I am at ccit ma
String starts with 'i', ends with 'i'? false
看了 使用Java的String类...的网友还看了以下:
如图所示,闭合开关S、S1,滑片放在R最左端时,R1与R的功率之比为2:3;闭合S、S2,滑片放在 2020-04-08 …
已知函数f(x)=2sin(x+3分之派)x属于R.写出函数f()的振幅、周期和初相.(2)函数f 2020-05-13 …
根据下列符号 指出原子中的质子数、中子数、质量数、核外电子数各为多少?.根据下列符号(符号为R,质 2020-05-16 …
短周期主族元素R的族序数是其周期序数的2倍,R在其最高正价氧化物中的质量分数为40%.已知R原子核 2020-05-22 …
一道关于通信原理的问题已知电话信道带宽3.4KHZ,S/N=20dB(=100),该信道上传输12 2020-06-14 …
单摆小球到达最低点的速度最大,加速度是否为0?如果是这样,但是小球运动到最低点时绳子的拉力最大,是根 2020-10-30 …
样地中现在布设了100个样点,怎么从测到的数据算出最少要多少个点就能符合最低要求?样地中现在布设了1 2020-11-11 …
给出一句英文句子(只由大小写字母和空格组成,不含标点符号,也不会出现连续的空格,第一个字符和最后一个 2020-12-02 …
上课听得好纠结..==以下2个题..写出最大值、最小值的自变量X的集合(1)y=1-1/2cos[( 2020-12-07 …
已知函数f(x)=cos2x/5+sin2x/5(x属于R),给出以下命题1.函数f(x)的最大值是 2020-12-08 …