Java String字符串相关知识

韩顺平字符串作业

  • homework01将字符串中的指定部分反转

    1. 题目:

      image-20240427032815284

    2. 首先编写一个静态方法

    3. 然后利用双指针进行不断对所给定参数 进行交换位置 其中需要用到交换辅助变量temp

    4. 然后返回一个String字符串

    5. 代码块:

      package hspedu.TestZeta;
      
      import java.util.Arrays;
      
      public class DemoTest {
          public static void main(String[] args) {
              String a="abcdef";
              String reverse = null;
              try {
                  reverse = reverse(a, 1, 8);
              } catch (Exception e) {
                  System.out.println(e.getMessage());
                  return;
              }
              System.out.println(reverse);
          }
      
          public static String reverse(String str,int start,int end){
      
              /*对输入数据 进行验证*/
              if(!(str!=null && start>=0 && end>start && end<str.length())){
                  throw new RuntimeException("参数不正确");
              }
      
              char[] chars=str.toCharArray();
              char temp=' ';//交换辅助变量
              for(int i=start,j=end;i<j;i++,j--){
                  temp=chars[i];
                  chars[i]=chars[j];
                  chars[j]=temp;
              }
      
      //      使用chars重新构建一个String返回
              return new String(chars);
          }
      
      }
      

  • homework02 字符串验证信息

    1. 题目:

    image-20240427032835549

    1. 先编写方法

    2. 针对 输入的内容进行核验 如果发现问题 就抛出异常 给出提示

    3. 单独写一个方法 判断密码是否全部都是数字字符 boolean

    4. 对于每个要求 只需找到其所对应的正确形式(最后在全部取反即可)

    5. 在main函数中 有可能抛异常的语句 要加上try-catch语句 只有没抛出异常 才可以进行try的下一个语句

    6. 代码块:

      package hspedu.TestZeta;
      
      import java.util.Arrays;
      
      public class DemoTest {
          public static void main(String[] args) {
              String name="null";
              String pwd="123256";
              String email="jacksouhu@.com";
      
              try {
                  userRegister(name,pwd,email);
                  System.out.println("恭喜你 注册成功");
              } catch (Exception e) {
                  System.out.println(e.getMessage());
              }
      
          }
      
          public static void userRegister(String name, String pwd, String email) {
      
              /*判断参数不为空*/
              if(!(name!=null && pwd!=null && email!=null)){
                  throw new RuntimeException("参数不能为空");
              }
              int length = name.length();
              /*先判断用户名*/
              if (!(length >= 2 && length <= 4)) {
                  throw new RuntimeException("用户名长度为2或3或4");
              }
      
              /*再判断密码*/
              if (!(pwd.length() == 6 && isDigital(pwd))) {
                  throw new RuntimeException("密码长度要为6 要求全数字");
              }
      
              /*判断邮箱*/
              int i = email.indexOf('@');
              int i1 = email.indexOf(".");
              if (!(i > -1 && i1 > i)) {
                  throw new RuntimeException("邮箱要求包含@和. 并且@在.之前");
              }
      
      
          }
      
          /*判断密码 是否全部为数字*/
          public static boolean isDigital(String pwd) {
              char[] charArray = pwd.toCharArray();
              for (int i = 0; i < charArray.length; i++) {
                  if (charArray[i] < '0' || charArray[i] > '9') {
                      return false;
                  }
              }
              return true;
          }
      }
      
      

    • 字符串统计

      1. 题目:

      image-20240427032852139

      1. 对输入的字符串进行校验

      2. 对输入的字符串进行按 空格分割split(" ") 得到一个String数组 分割后判断数组长度是否符合要求(3)

      3. 对分割后的数据 格式化 String.format()

      4. 代码块

        package hspedu.TestZeta;
        
        import java.util.Arrays;
        
        public class DemoTest {
            public static void main(String[] args) {
                String name="Han Shun Ping";
                printName(name);
            }
        
            /*完成输出格式要求的字符串*/
            public static void printName(String str){
                 if(str==null){
                     System.out.println("输入不能为空");
                     return;
                 }
        
                 /*对输入用空格进行分割*/
                String[] names=str.split(" ");
                if(names.length!=3){
                    System.out.println("输入的格式不正确");
                }
        
        //      /*对于最后一个 先把中间的全部转成大写 再取出第一个字符*/
                /*String数组可以用charAt(0)取出某个索引位置的char字符*/
                String format=String.format("%s,%s .%c",names[2],names[0],names[1].toUpperCase().charAt(0));
                System.out.println(format);
            }
        }
        

        • 判断大小写字母个数

          1. 题目:

            image-20240427032902575

          2. 遍历字符串 如果char 在‘0’~ ‘9’ 就是一个数字

          3. 如果char 在’a’-‘z’ 就是一个小写字母

          4. 如果在 ‘A’-‘Z’ 就是一个大写字母

          5. 分别使用三个变量 保存以上三个结果

          6. 代码块:

            package hspedu.TestZeta;
            
            import java.util.Arrays;
            
            public class DemoTest {
                public static void main(String[] args) {
                    String str="abcHsp U 1234";
                    countStr(str);
                }
            
                public static void countStr(String str) {
                    if (str == null) {
                        System.out.println("输入不能为空");
                        return;
                    }
            
                    int length = str.length();
                    int num = 0;
                    int low = 0;
                    int up = 0;
                    int other = 0;
            
                    /*String 不能 直接用下标去取出来字符 可以用charAt(索引)来取*/
            
                    /*遍历*/
                    for (int i = 0; i < length; i++) {
                        if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
                            num++;
                        } else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
                            low++;
                        } else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
                            up++;
                        } else other++;
                    }
            
                    System.out.println("数字个数:" + num);
                    System.out.println("小写字母:" + low);
                    System.out.println("大写字母:" + up);
                    System.out.println("其他:" + other);
                }
            }
            

            • 根据代码得结果

              1. 题目:

                image-20240427030813053

              2. s1 指向常量池中的hepedu

              3. a 指向堆中的name

              4. b 指向堆中的 name2

              5. 并且 这两个堆中的name和name2都指向常量池中的hspedu

              6. a和b 指向堆中 不同的name对象

              7. a.equal(b) 判断a和b是不是同一个对象 由于Animal 未重写equal方法

              8. s4是new 出来的 所以s4在堆中 一定有空间 Value数组(String真正存放字符序列)这个Value还是指向常量池中的hspedu

              9. s5 是直接指向常量池中的hspedu

              10. s1和s4 指向不同 所以false

              11. s4和s5 指向也不同 所以false

              12. t1 是先指向堆中的Value2 这个Value2 指向常量池中的hellihspedu

              13. t1.intern() 是直接指向常量池的 所以返回true

              14. 代码块:

                 package hspedu.TestZeta;
                
                import java.util.Arrays;
                
                public class DemoTest {
                    public static void main(String[] args) {
                        String s1="hspedu";
                        Animal a=new Animal(s1);
                        Animal b = new Animal(s1);
                        System.out.println(a==b);
                        System.out.println(a.equals(b));
                        System.out.println(a.name== b.name);
                        String s4=new String("hspedu");
                        String s5="hspedu";
                        System.out.println(s1==s4);
                        System.out.println(s4==s5);
                
                        String t1="hello"+s1;
                        String t2="hellohspedu";
                        System.out.println(t1.intern()==t2);
                    }
                
                }
                
                class Animal{
                    String name;
                    public Animal(String name) {
                        this.name = name;
                    }
                }
                

  • 23
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值