1、检查输入的字符串是否为”回文”,返回boolean结果。2、convert2Array()方法将传入的字符串转换为字符数组返回。提示:不能使用toCharArray() 方法。3、countX()方法统计参数字符串中字符x出现的次数,返回int结果。4、dispalyCount()统计传入的字符串中的字母、数字以及其它字符的个数,并显示。5、convertAndSqrt()将传入的数字字符串转换为数值,返回该数值的平方根。将、6入的一个语句,进行单词内的反向输出 例如:I love this game 输出: I evol siht emag
package com.My.chapter01;
import javax.swing.JOptionPane;
publicclass TestString02 {
privatestaticint index;
publicstaticvoidmain(String[] args) {
String str;
isPalindrome("avff");
convert2Array("122133dfdf");
countX("abcxaaax");
dispalyCount("123cxddgj**^%");
convertAndSqrt("123");
Reverse("I love this game");
}
// 2、isPalindrome ()方法能够检查输入的字符串是否为”回文”,返回boolean结果。publicstatic boolean isPalindrome(String a) {
boolean flag = true;
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) == a.charAt(a.length() - i - 1)) {
flag = true;
} else {
flag = false;
}
}
System.out.println(flag);
System.out.println();
return flag;
}
// 3,convert2Array()方法将传入的字符串转换为字符数组返回。提示:不能使用toCharArray() 方法。publicstaticchar[] convert2Array(String a) {
char b[] = newchar[a.length()];
for (int i = 0; i < a.length(); i++) {
b[i] = a.charAt(i);
}
System.out.println(b);
System.out.println();
return b;
}
// 4,countX()方法统计参数字符串中字符x出现的次数,返回int结果。publicstaticintcountX(String a) {
int count = 0;
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) == 'x') {
count++;
}
}
System.out.println("字符串中的x个数为: " + count);
return count;
}
// 5,dispalyCount()统计传入的字符串中的字母、数字以及其它字符的个数,并显示。publicstaticvoiddispalyCount(String a) {
int x = 0; // 统计字母个数int y = 0; // 统计数字个数int z = 0; // 统计其他字符个数char charNumber[] = newchar[a.length()];
int number[] = newint[a.length()];
char elseNumber[] = newchar[a.length()];
for (int i = 0; i < a.length(); i++) {
char b = a.charAt(i);
if (b >= 48 && b <= 56) {
number[y] = b - 48;
y++;
} elseif ((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')) {
charNumber[x] = b;
x++;
} else {
elseNumber[z] = b;
z++;
}
}
System.out.print("字母个数为: " + x + " ");
System.out.print("字符串中的字母为 : ");
for (int i = 0; i < charNumber.length; i++) {
System.out.print(charNumber[i] + " ");
}
System.out.println();
System.out.print("数字个数为: " + y + " ");
System.out.print("字符串中的数字为 : ");
for (int i = 0; i < y; i++) {
System.out.print(number[i] + " ");
}
System.out.println();
System.out.print("其他字符个数为: " + z + " ");
System.out.print("字符串中的其他字符为 : ");
for (int i = 0; i < elseNumber.length; i++) {
System.out.print(elseNumber[i] + " ");
}
System.out.println();
System.out.println();
}
// 6,convertAndSqrt()将传入的数字字符串转换为数值,返回该数值的平方根。publicstaticdoubleconvertAndSqrt(String a) {
double result = 0;
if (a.equals(0)) {
result = 0;
} else {
int x = Integer.parseInt(a);
result = Math.sqrt(x);
System.out.println(x + " 的平方根为 " + (int) (result * 100 + 0.5) / 100.0);
}
return result;
}
// 练习: 将输入的一个语句,进行单词内的反向输出 例如:I love this game 输出: I evol siht emagpublicstaticvoidReverse(String a) {
String str=" I love this game";
str=str.trim()+" ";
//利用空格个数 ,计数单词int count = 0;
int index = str.indexOf(" ");
while(index != -1){
count++;
index=str.indexOf(" ",index+1);
}
//定义新数组放每个单词
index = 0;
int start = 0;
int end = str.indexOf(" ");
String newStr[]=new String[count];
while(end != -1){
newStr[index++] = str.substring(start,end);
end = str.indexOf(" ",end+1);
}
}
}