/**
* 编程,已知字符串:“this is a test of java”.
* 要求执行以下操作:
* 1)统计改字符串中字母s出现的次数
* 2)取出子字符串“test”
* 3)用多种方式将字符串复制到一个字符数组Char[] str中
* 4)将字符串中每个单词的第一个字母変成大写,输出到控制台
* 5)用两种方式实现该字符串的倒叙输出(用StringBuffer和for循环方式分别实现)
* 6)将本字符串转换成一个字符串数组,要求每个数组元素都是一个有意义的英文单词,并输出到控制台
*/
import java.util.Arrays;
public class TsetStringExec2 {
private static String str = "this is a test of java";
public static void main(String[] args) {
test1();
test2();
test3();
test4();
test5();
}
public static void test1(){
int count = 0;
for (int i = 0; i < str.length(); i++) {
if('s'==str.charAt(i)){
count++;
}
}
System.out.println("s出现的次数"+count);
}
public static void test2(){
int start = str.indexOf("test");
String s = str.substring(start,start+5);
System.out.println("取出"+s);
}
public static void test3(){
char[] ch = new char[str.length()];
for (int i = 0; i < str.length(); i++) {
ch[i] = str.charAt(i);
}
System.out.println(Arrays.toString(ch));
char[] ch2 = str.toCharArray();
System.out.println(Arrays.toString(ch2));
}
public static void test4(){
String[] ss = str.split(" ");//其実用空格并不准确,应该使用正则表达式
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ss.length; i++) {
String s = ss[i].substring(0,1).toUpperCase()+ss[i].substring(1);
sb.append(s);
}
System.out.println(sb);
}
public static void test5(){
StringBuffer sb = new StringBuffer(str);
System.out.println(sb.reverse().toString());
StringBuffer sb2 = new StringBuffer();
for (int i = str.length()-1; i >=0; i--) {
sb.append(str.charAt(i));
}
System.out.println("--"+sb2);
}
}
* 编程,已知字符串:“this is a test of java”.
* 要求执行以下操作:
* 1)统计改字符串中字母s出现的次数
* 2)取出子字符串“test”
* 3)用多种方式将字符串复制到一个字符数组Char[] str中
* 4)将字符串中每个单词的第一个字母変成大写,输出到控制台
* 5)用两种方式实现该字符串的倒叙输出(用StringBuffer和for循环方式分别实现)
* 6)将本字符串转换成一个字符串数组,要求每个数组元素都是一个有意义的英文单词,并输出到控制台
*/
import java.util.Arrays;
public class TsetStringExec2 {
private static String str = "this is a test of java";
public static void main(String[] args) {
test1();
test2();
test3();
test4();
test5();
}
public static void test1(){
int count = 0;
for (int i = 0; i < str.length(); i++) {
if('s'==str.charAt(i)){
count++;
}
}
System.out.println("s出现的次数"+count);
}
public static void test2(){
int start = str.indexOf("test");
String s = str.substring(start,start+5);
System.out.println("取出"+s);
}
public static void test3(){
char[] ch = new char[str.length()];
for (int i = 0; i < str.length(); i++) {
ch[i] = str.charAt(i);
}
System.out.println(Arrays.toString(ch));
char[] ch2 = str.toCharArray();
System.out.println(Arrays.toString(ch2));
}
public static void test4(){
String[] ss = str.split(" ");//其実用空格并不准确,应该使用正则表达式
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ss.length; i++) {
String s = ss[i].substring(0,1).toUpperCase()+ss[i].substring(1);
sb.append(s);
}
System.out.println(sb);
}
public static void test5(){
StringBuffer sb = new StringBuffer(str);
System.out.println(sb.reverse().toString());
StringBuffer sb2 = new StringBuffer();
for (int i = str.length()-1; i >=0; i--) {
sb.append(str.charAt(i));
}
System.out.println("--"+sb2);
}
}