第7题做的不好...
package HomeWork;
import java.util.Arrays;
/*3.
String str=" ac,42,123,sd Fa,c df,4,acdf,5ewRRre ";
1.把字符串按,进行分割
2.把里面的大写转化成小写
3.把里面的小写转化成大写
4.统计字符串的长度
5.把字符串前后的空格去掉
6.把字符串中所有空格去掉
7. 找出字符串中所有a出现的位置,没有返回-1*/
public class HomeWork_03 {
public static void main(String[] args) {
String str = " ac,42,123,sd Fa,c df,4,acdf,5ewRRre ";
char[] key = str.toCharArray();
show1(str, ",");
show2(str);
show3(str);
show4(str);
show5(str);
show6(str, " ");
show7(str, "x");
show7(str, "a");
}
public static void show1(String str, String key) {// (1)
String[] s1 = str.split(key);// 分割后-存到数组-在输出
sop("割掉,:");
for (int i = 0; i < s1.length; i++) {
sop(s1[i]);
}sop("\n");
}
public static void show2(String str) {// (2)
sop("转小写:" + str.toLowerCase() + "\n");
}
public static void show3(String str) {// (3)
sop("转大写:" + str.toUpperCase() + "\n");
}
public static void show4(String str) {
char[] key = str.toCharArray();// (4)存到数组在遍历(定义个计数器)
int sum = 0;
for (int i = 0; i < key.length; i++) {
sum++;
}
sop("长度:" + sum + "\n");
}
public static void show5(String str) {
sop("去前后空格:"+str.trim() + "\n");
}
public static void show6(String str, String key) {
// 截取后重新组合的新串
String[] sc = str.split(key);// 先割掉
for (int i = 0; i < sc.length; i++) {// 在循环输出(组合)
sop(sc[i]);
}
sop("\n");
}
public static void show7(String str, String key) {
int index = 0;
index = str.indexOf(key, index);
if (index != -1) {
while ((index = str.indexOf(key, index)) != -1) {
sop("位置码:"+index + "\n");
index = index + key.length();
}
} else {
sop("位置码: -1\n");
}
}
public static void sop(Object obj) {
System.out.print(obj);
}
}