<pre name="code" class="java">package homework;
import java.util.Arrays;
//1.编程. 已知字符串:"this is a test of java".
//按要求执行以下操作:
// (1) 统计该字符串中字母s出现的次数
// (2) 取出子字符串"test"
// (3) 将本字符串复制到一个字符数组Char[] str中.
// (4) 将字符串中每个单词的第一个字母变成大写, 输出到控制台。
// (5) 用两种方式实现该字符串的倒叙输出。(用StringBuffer和for循环方式分别实现)
// (6) 将本字符串转换成一个字符串数组,要求每个数组元素都是一个有意义的额英文单词,并输出到控制台
public class Work2 {
public static void main(String[] args) {
String a = "this is a test of java";
System.out.println(count(a));
System.out.println(sub(a));
System.out.println(change(a));
dao(a);
System.out.println();
System.out.println(daoo(a));
System.out.println(Arrays.toString(shuzu(a)));
}
public static int count(String aa){
char[] b = aa.toCharArray();//将字符串抓换为字符数组
int sum = 0;
for (int i = 0; i < b.length; i++) {
if (b[i] == 's') {
sum++;
}
}
return sum;
}
public static String sub(String aa){
String s = aa.substring(10, 14);//字符串的截取
return s;
}
public static char[] copy(String aa){
char[] c = aa.toCharArray();
return c;
}
public static char[] change(String aa){
char []b = aa.toCharArray();
b[0] = (char) (b[0] - 32);
for (int i = 1; i < b.length; i++) {
if (b[i] == ' ') {
b[i + 1] = (char) (b[i + 1] - 32);
}
}
return b;
}
public static void dao(String aa){
char[] c = aa.toCharArray();
for (int i = c.length - 1; i >= 0; i--) {
System.out.print(c[i]);
}
}
public static StringBuffer daoo(String aa){
StringBuffer buffer = new StringBuffer(aa);//创建StringBuffer对象,进行字符串翻转
StringBuffer str = buffer.reverse();
return str;
}
public static String[] shuzu(String aa){
String str[] = aa.split(" ");//拆分字符串放置到字符串数组
return str;
}
}
常用类练习提
最新推荐文章于 2022-06-08 14:37:45 发布