早教吧作业答案频道 -->其他-->
使用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类...的网友还看了以下:
我国古代有政治家,立谏自己的君主,战胜于朝廷的事例你能举出一两个吗?你认为他们的劝说方式,对今天的 2020-05-22 …
把鸡蛋两头敲出小口然后吹一头能吹出一整个鸡蛋的原理是什么? 2020-06-02 …
我刚刚接触初三的电路,到底如何分辨是并联还是串联,电压表和电流表在电路中测得到底是哪一个元件(注: 2020-06-05 …
有红、黄、蓝色小球各6个,混合放在一个不委透明的盒子里,摸出一10个球,问:其中至少有几个小球的颜 2020-06-20 …
《草原情》详尽的描写了蒙古族的风情,请各举出一两个例子,体现在饮食上的有(),服装上(),娱乐上( 2020-06-25 …
洛克菲勒在给儿子的一封信说:“成功地将一个好主意付诸实践,比在家空想出一千个好主意更有价值。”请以 2020-07-23 …
(1)狼是我们十分熟悉的一种凶猛的动物,它经常出现在文学作品中,关于狼的故事很多,你能写出一两个吗 2020-07-24 …
《庄子》一书有一个很重要的艺术特点,就是善于运用语言,运用寓言来说明某个道理活讽刺某类人、某件事. 2020-07-25 …
英语好的进来帮下忙!我们要进行一个对外国人的的调查.要设计一个调查问表,自己英语水平有限,请大家帮个 2020-11-27 …
一道初中数学题某种玩具进价一,若按八元一个销售,每天可卖出一百个,若每个售价涨一元,则日销售量减少两 2020-12-03 …