字符串

作者: Y_Jaaao | 来源:发表于2018-11-18 15:02 被阅读0次

    字符串学习

    1.定义

    • 概念:字符串或串(String)是由数字、字母、下划线组成的一串字符,一般记为s=“123456”是编程语言中表示文本的数据类型
    • 注意

    2.常用方法

    • s.length();求字符串长度
    • s.endsWith(.jpg);判断字符串是否是.jpg结尾

    3.使用场景

    • 文件名重命名
      代码
      ...
      public static void main(String[] args) {
      String fileName = "11.jpg";
      //取出.jpg子串
      String s1 =fileName.substring(2);
      System.out.println(s1);
      //算出随机产生.jpg前的位数
      System.out.println(UUID.randomUUID().toString().length());
      //用UUID生成主文件名
      String newFileName = UUID.randomUUID().toString()+s1;
      System.out.println(newFileName);
      }

      ... m1.PNG
    • 禁词过滤
      代码
      ...
      public static void main(String[] args) {
      String content = "床前明月光,疑是地上霜。" +
      "举头望明月,低头思故乡。";
      //字符串的替换
      String finalStr = content.replaceAll("明月","**");
      System.out.println(finalStr);
      }
      ...

      m1.PNG
    • 文件类型统计
      代码:
      ...
      int imgCount = 0;
      int docCount = 0;
      for (String fileName:fileNames) {
      if (fileName.endsWith(".jpg")||fileName.endsWith(".png")||fileName.endsWith(".bmp")){
      //统计文档数量
      imgCount++;
      }
      if (fileName.endsWith(".pdf")||fileName.endsWith(".exe")||fileName.endsWith(".doc")){
      docCount++;
      }
      }

      ... m1.PNG
    • 正则表达式
      代码:
      ...
      //输入一个密码
      String password = "Yj097386";
      //给密码指定范围
      String regexp = "[0-9A-Za-z]{6,12}";
      //进行验证
      boolean flag = password.matches(regexp);
      System.out.println(flag);
      ...


      m1.PNG

    相关文章

      网友评论

        本文标题:字符串

        本文链接:https://www.haomeiwen.com/subject/qjvdfqtx.html