一、定义一个方法将字符串首字母变为大写后加上get前缀
Test01
package com.home.demo01;
public class Test01 {
public static void main(String[] args) {
System.out.println(getStr("asfavasvcsa"));
}
public static String getStr(String str) {
String s1 = str.substring(0, 1).toUpperCase();
//截取首字母变大写
String s2 = str.substring(1);
//截取后面的字母
return "get" + s1 + s2;
//拼接
}
}
二、定义一个数字字符串数组;定义一个方法判断一个数是否对称数
package com.home.demo01;
public class Test02 {
public static void main(String[] args) {
String [] arrays = {"010","3223","666","7890987","123123"};
for (int i=0;i<arrays.length;i++){
if (isFlag(arrays[i])){
System.out.println(arrays[i]);
}
}
}
public static boolean isFlag(String s){
StringBuilder sb=new StringBuilder(s);
boolean equals = sb.reverse().toString().equals(s);
return equals;
}
}
三、键盘录入字符串,输入end结束;字符串中大写小写互换,其他用*代替,并统计字母的总个数
Test03
package com.home.demo01;
import java.util.Scanner;
public class Test03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
boolean flag = true;
do {
System.out.println("输入字符串");
String s = sc.next();
if (s.equalsIgnoreCase("end")) {
flag = false;
} else {
sb.append(s);
}
} while (flag);
StringBuilder sb01 = new StringBuilder();
char[] chars = sb.toString().toCharArray();
for (char aChar : chars) {
if (aChar >= 'A' && aChar <= 'Z') {
sb01.append((aChar + "").toLowerCase());
} else if (aChar >= 'a' && aChar <= 'z') {
sb01.append((aChar + "").toUpperCase());
} else {
sb01.append("*");
}
}
System.out.println(sb01);
}
}
四、定义一个方法,参数:数组;功能:转换成字符串,例{1,2,3}变成[1,2,3]。
Test04
package com.home.demo01;
public class Test04 {
public static void main(String[] args) {
int[] arrays = {2, 3, 8, 6, 5, 3};
change(arrays);
}
public static void change(int[] arrays) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arrays.length; i++) {
if (i == 0) {
sb.append("[" + arrays[i] + ",");
} else if (i == arrays.length - 1) {
sb.append(arrays[i] + "]");
} else {
sb.append(arrays[i] + ",");
}
}
System.out.println(sb);
}
}
五、定义一个方法,参数:字符串,功能:返回反转后的字符串。
package com.home.demo01;
public class Test05 {
public static void main(String[] args) {
String s = "asfcabvsbaef";
System.out.println(res(s));
}
public static String res(String s) {
StringBuilder sb = new StringBuilder(s);
return sb.reverse().toString();
}
}