一:String反转

image.png

  1. public class Test1 {
  2. public static void main(String[] args) {
  3. String string = "abcdef";
  4. try {
  5. System.out.println(reverse(string, -1, 4));
  6. } catch (Exception e) {
  7. System.out.println(e.getMessage());;
  8. }
  9. }
  10. public static String reverse(String str, int start, int end) {
  11. //还可以添加验证
  12. //1: 先想出正确的
  13. //2: 然后再取反(因为正确的容易想到)
  14. if (!(str != null && start > end && start < str.length() &&
  15. end < str.length() && start != end && start > +0)) {
  16. throw new RuntimeException("参数不正确");
  17. }
  18. //先将String转换为char类型数组
  19. char[] chars = str.toCharArray();
  20. for (int i = start, j = end; i < j; i++, j--) {
  21. char temp = chars[i];
  22. chars[i] = chars[j];
  23. chars[j] = temp;
  24. }
  25. return new String(chars);
  26. }
  27. }

二:注册处理

image.png

三:字符串统计

image.png

  1. public class Test3 {
  2. public static void main(String[] args) {
  3. String name = "Han shun Ping";
  4. System.out.println(printName(name));
  5. }
  6. public static String printName(String str) {
  7. String[] s = str.split(" ");
  8. String resultString = String.format("%s,%s .%c" ,s[2],s[0],s[0].toUpperCase().charAt(0));
  9. return resultString;
  10. }
  11. }

四:统计字符串

image.png

  1. public class Test4 {
  2. public static void main(String[] args) {
  3. String str = "saddfHOHN 1q534215";
  4. countSting(str);
  5. }
  6. public static void countSting(String str){
  7. int NumCount = 0;
  8. int LowerCount = 0;
  9. int UpCount = 0;
  10. int Other = 0;
  11. for (int i = 0; i < str.length(); i++) {
  12. if(str.charAt(i)>='A' && str.charAt(i)<= 'Z'){
  13. UpCount++;
  14. }else if (str.charAt(i)>='0' && str.charAt(i)<= '9'){
  15. NumCount++;
  16. }else if (str.charAt(i)>='a' && str.charAt(i)<= 'z'){
  17. LowerCount++;
  18. }else {
  19. Other++;
  20. };
  21. }
  22. System.out.println("数字有:"+ NumCount);
  23. System.out.println("大写字母有:"+ UpCount);
  24. System.out.println("小写字母有:"+ LowerCount);
  25. System.out.println("其他有:"+ Other);
  26. }
  27. }

五:

image.png
image.png