Java :查找次数和连接(两种方法),String方法图,替换,相等,提取,输入,翻转









//练习内容一:编写一个Java程序,完成以下功能:
// 声明一个名为s的String对象,并使它的内容是"Call me Accp";
// 打印整个字符串。
// 使用length()方法打印字符串的长度
// 打印字符串的第一个字符
// 打印字符串的最后一个字符
// 打印字符串的第一个单词。
// 打印字符串的最后一个单词。
package a;
public class fuxi {
public static void main(String[] args) {
String s = "Call me Accp";
int len = s.length();
char charAt = s.charAt(0);
char charAt2 = s.charAt(len - 1);
System.out.println("打印整个字符串: " + s);
System.out.println("Call me Accp的长度 : " + len);
System.out.println("Call me Accp的第一个字符 : " + charAt);
System.out.println("Call me Accp的最后一个字符 : " + charAt2);
String a = s.substring(s.lastIndexOf(" ") + 1);
String a1 = s.substring(s.lastIndexOf(" "));
String a2 = s.substring(s.lastIndexOf(" ") + 2);
String a3 = s.substring(s.lastIndexOf(" ") - 1);
String b = s.substring(0, 5);
System.out.println("字符串的第一个单词:" + b);
System.out.println("字符串的最后一个单词" + a);
}
}



// 练习内容二:编写一个Java程序,用于
// 测试两个字符串String firstString="This is"和String secondString="This is";是否相等。
// 将"his father"与其中的firstString字符串连接。
// 用z替换新字符串中的i。
方法一:
package a;
public class fuxi {
public static void main(String[] args) {
String S1 = "This is";
String S2 = "This is";
if (S1.equals(S2)) {
System.out.println("字符串S1和S2相等");
}
else {
System.out.println("字符串S1和S2不相等");
}
String[] arr = { S1, "his father" };
String d = arr[0] + arr[1];/* 下标一定要对 */
System.out.println(d);
System.out.println("用z替换新字符串d中的i后的结果:" + d.replace('i', 'z'));
}
}


方法二:
package a;
public class fuxi {
public static void main(String[] args) {
String S1 = "This is";
String S2 = "This is";
if (S1.equals(S2)) {
System.out.println("字符串S1和S2相等");
}
else {
System.out.println("字符串S1和S2不相等");
}
String[] arr = { S1, "his father" };
String d = arr[0].concat(arr[1]);/* 下标一定要对 */
System.out.println("S1和结果his father连接的结果:" + d);
System.out.println("用z替换新字符串d中的i后的结果:" + d.replace('i', 'z'));
}
}



// 练习三:编写一个java程序,从字符串"wellcom to Beijing"中提取单词"well"和"Beijing".(使用subString()来做)
package a;
public class fuxi {
public static void main(String[] args) {
String a = "wellcom to Beijing";
String d = a.substring(0, 7);/* 下标一定要对 */
String c = a.substring(a.lastIndexOf(" ")
);/* 下标一定要对,a.lastIndexOf(" ")表明空格后面的字符,substring表明截取 */
System.out.println("提取单词well:" + d);
System.out.println("提取单词Beijing:" + c);
}
}



练习内容四:构建一个Occurrences程序,查找字符"e"在字符串"Seize the day"中出现的次数。
方法一:
package a;
public class fuxi {
public static void main(String[] args) {
String str = "Seize the day";
int count = 0;
// 统计出现了多少次的变量
int start = 0;// 每次查找开始的位置
while ((start = str.indexOf("e", start)) != -1) {
count++;// 如果循环一次出现 的次數就+1
start++;// 并且让其开始的位置+1,如果不+1就会出现死循环
}
System.out.println("e在Seize the day中出现了" + count + "次");
}
}


方法二:
package a;
public class fuxi {
public static void main(String[] args) {
String str = "Seize the day";
int times = searchstr("e", str);
System.out.println("字符“e”在字符串“Seize the day”中出现的次数:" + times);
}
public static int searchstr(String key, String str) {
int index = 0;
int count = 0;
while ((index = str.indexOf(key, index)) != -1) {
index = index + key.length();
count++;
}
return count;
}
}


/*练习内容五:编写一个ExtractString程序,从字符串"Seize the Day"中提取单词"the"和"Day".*/
package a;
public class fuxi {
public static void main(String[] args) {
String a = "Seize the Day";
String d = a.substring(6, 9);
String c = a.substring(10);
System.out.println("提取单词the:" + d);
System.out.println("提取单词Day:" + c);
}
}


// 练习六:"今天的天气不错,虽然天气不错,但是我希望天气能够更好点",统计"天气"出现的次数
package a;
public class fuxi {
public static void main(String[] args) {
String str = "今天的天气不错,虽然天气不错,但是我希望天气能够更好点";
int times = searchstr("天气", str);
System.out.println("字符“天气”在字符串“今天的天气不错,虽然天气不错,"
+ "但是我希望天气能够更好点”中出现的次数:" + times);
}
public static int searchstr(String key, String str) {
int index = 0;
int count = 0;
while ((index = str.indexOf(key, index)) != -1) {
index = index + key.length();
count++;
}
return count;
}
}


// 练习七:要求用户输入一个字符串,对其进行翻转
package a;
import java.util.Scanner;
public class fuxi {
public static void main(String[] args) {
System.out.println("请输入一个字符串");
Scanner shurudeneirong = new Scanner(System.in);
String jieshoudeneirong = shurudeneirong.next();
String reverse = new StringBuffer(jieshoudeneirong).reverse()
.toString();
System.out.println("字符串反转前:" + jieshoudeneirong);
System.out.println("字符串反转后:" + reverse);}}

