判断一个对象是否属于一个类可以用关键字instanceof,它是二元操作符,格式为:

对象 instanceof 类名

式子的值为一个布尔值(boolean)

Object sth;
boolean isString = sth instanceof String;

或者

if (sth instanceof String) {// your code
}
问题:获取文件格式
package cn.practice3;import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;public class StrFileQuestion {public static void main(String[] args) {//File f = new File("d:/download/spring.jpg");//System.out.println(f.getAbsolutePath());//System.out.println(File.separator);//String p = "d:\\xxx\\user\\user_abc.jpg";//System.out.println(p);//String fc = File.separator; // \//System.out.println(fc);//fc = p.indexOf(fc) == -1 ? "/" : fc;//System.out.println(fc);////System.out.println("hello".substring(0,2));String p = "d:/xxx/user/user_abc.jpg";System.out.println(p);//File.separator 是根据系统返回相关的符号 linux / windows \ \\String fc = File.separator; // \fc = p.indexOf(fc) == -1 ? "/" : fc;System.out.println(fc);String path = p.substring(0, p.lastIndexOf(fc)).concat(fc);System.out.println(path);String name = p.substring(p.lastIndexOf(fc) + 1);System.out.println(name);String ext = name.lastIndexOf(".") == -1 ? "" : name.substring(name.lastIndexOf(".") + 1);System.out.println(ext);UUID uuid = UUID.randomUUID();String newp = String.format("%s%s.%s", path, uuid, ext);System.out.println(newp);SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssS");Random rand = new Random();String num4 = String.format("%05d",rand.nextInt(99999));System.out.println(num4);System.out.printf("%s%s-%s.%s",path,sdf.format(new Date()),num4,ext);}
}

6.2 StringBuilder(StringBuffer)

它们都是可变字符串,使用上功能上基本一样,StringBuilder不支持多线程(非线程安全),它们

都支持动态修改。

序号 方法描述
1 public StringBuffer append(String s) 将指定的字符串追加到此字符序列。
2 public StringBuffer reverse() 将此字符序列用其反转形式取代。
3 public StringBuilder delete(int start, int end) 移除此序列的子字符串中的字符。
4 public insert(int offset, int i) 将 int 参数的字符串表示形式插入此序列中。
5 insert(int offset, String str) 将 str 参数的字符串插入此序列中。
6 replace(int start, int end, String str) 使用给定 String 中的字符替换此序列的子字符串
7 int capacity()返回当前容量
8 char charAt(int index) 返回此序列中指定索引处的 char 值。
9 void ensureCapacity(int minimumCapacity) 确保容量至少等于指定的最小值
10 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 将字符从此序列复制到目标字符数组 dst
11 int indexOf(String str) 返回第一次出现的指定子字符串在该字符串中的索引
12 int indexOf(String str, int fromIndex) 从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。
13 int lastIndexOf(String str) 返回最右边出现的指定子字符串在此字符串中的索引。
14 int lastIndexOf(String str, int fromIndex) 返回 String 对象中子字符串最后出现的位置。
15 int length() 返回长度(字符数)
16 void setCharAt(int index, char ch) 将给定索引处的字符设置ch
17 void setLength(int newLength) 设置字符序列长度
18 CharSequence subSequence(int start, int end) 返回一个新的字符序列,该字符序列是此序列的子序列
19 String substring(int start) 返回一个新的 String ,它包含此字符序列当前所包含的字符子序列
20 String substring(int start, int end) 返回一个新的 String ,它包含此序列当前所包含的字符子序列。
21 String toString() 返回此序列中数据的字符串表示形式。
package cn.practice3;public class StringBuilderDemo {public static void main(String[] args) {m1();StringBuilder sbu = new StringBuilder();sbu.append("hello");sbu.append(18);sbu.insert(1,"Welcome - ");//sbu.append("Welcome - ");//sbu.append("Welcome - ",0,"Welcome - ".length());System.out.println(sbu);//hWelcome - ello18System.out.println(sbu.toString().concat("java"));//hWelcome - ello18javasbu.delete(0,5);System.out.println(sbu);//ome - ello18//错误的//String s = sbu;//String 转换为 StringBuilderString s1 = "hello";StringBuilder ss1 = new StringBuilder(s1);System.out.println(s1);//helloSystem.out.println(ss1);//helloSystem.out.println(ss1.toString());//helloSystem.out.println(ss1.reverse().append("java.18").toString().toUpperCase());//OLLEHJAVA.18//StringBuilder 转换为 StringString s2 = ss1.toString();System.out.println(s2);//ollehjava.18}public static void m1() {StringBuilder su1 = new StringBuilder();StringBuilder su2 = new StringBuilder(6);StringBuilder su3 = new StringBuilder("hello");System.out.println(su1);//空System.out.println(su1.length());//0System.out.println(su2);//空System.out.println(su2.length());//0System.out.println(su3);//helloSystem.out.println(su3.length());//5StringBuffer suf1 = new StringBuffer();StringBuffer suf2 = new StringBuffer(6);StringBuffer suf3 = new StringBuffer("hello");System.out.println(suf1);//空System.out.println(suf1.length());//0System.out.println(suf2);//空System.out.println(suf2.length());//0System.out.println(suf3);//helloSystem.out.println(suf3.length());//5}
}

面试题:

  1. 说明StringBuffer 和 StringBuilder的区别?
    在java_note2中有详解StringBuffer 和 StringBuilder的区别

  2. String、StringBuilder、StringBuffer的 ?

小练习:
写程序输出如下字符串
1-2345+6+78+9
12-345-6-7+89
1+2-3-4-5-67+89
1-23+45+67+8-9
1+2-3+456+7+89
1+23-4+567-8-9
12-34-5+6+7+8+9
package cn.practice3;import java.util.Random;public class RandomStr {public static void main(String[] args) {//随机生成如下表达式  12+345-678+9 = 100//1-23+5-6-7+89   12+345-678+9for (int i = 0; i < 10; i++) {System.out.println(getNums());}}public static String getNums() {Random rand = new Random();String[] os = {"", "-", "+"};StringBuilder s = new StringBuilder();for (int i = 1; i < 9; i++) {s.append(String.format("%d%s", i, os[rand.nextInt(os.length)]));}s.append("9");return s.toString();}
}

package cn.practice3;import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Regexp2 {public static void main(String[] args) {int i = 0;int n = 0;int t = 0;while (true) {++t;String s = getNums();if (isOk(s)) {System.out.printf("%s=100(循环%d次)%n", s, t);++i;n += t;t = 0;}if (i >= 11) break;}System.out.println(n);}public static boolean isOk(String str) {Pattern p = Pattern.compile("-?\\d+");Matcher m = p.matcher(str);int sum = 0;while (m.find()) {String t = m.group();sum += Integer.parseInt(t);}return sum == 100;}public static String getNums() {Random rand = new Random();String[] os = {"", "-", "+"};StringBuilder s = new StringBuilder();for (int i = 1; i < 9; i++) {s.append(String.format("%d%s", i, os[rand.nextInt(os.length)]));}s.append("9");return s.toString();}
}

6.3正则表达式(regexp)

正则表达式定义了字符串的模式,本质是一种特殊的字符串对象。

正则表达式可以用来搜索、编辑或处理文本。

正则表达式并不局限于某一种语言,但是在每种语言中有细微的差别。

正则表达式实例

一个字符串其实就是一个简单的正则表达式,eg. Hello World 正则表达式匹配 “Hello World” 字符串。

.(点号)也是一个正则表达式,它匹配任何一个字符如:“a” 或 “1”

作业:

  1. 判断字符串有没有大写字母
  2. 判断字符串有没有汉字
  3. 判断字符串有没有数字
  4. 判断一个字符串是不是标准的手机号
package cn.practice3;public class Regexp1 {public static void main(String[] args) {//判断字符串是中文还是英文
//        String str = "中国";
//        String str1 = "hello4mysql";
//        byte[] bs = str.getBytes();//1.判断字符串有没有大写字母String s1 = "Hello123";//不使用正则表达式boolean f = false;for (char c:s1.toCharArray()){if (c>='A'&& c<='Z'){f = true;break;}}System.out.println(f?"有大写字母":"没有");//使用正则表达  .*[A-Z].*    .代表一个任意字符   *代表{0,}    [A-Z]{3}System.out.println(s1.matches(".*[A-Z].*")?"有大写字母":"没有");//2.判断字符串有没有汉字String s2 = "Hello,中programmer";System.out.println(s2.matches(".*[\\u4e00-\\u9fa5].*") ? "有汉字" : "没有");//3.判断字符串有没有数字 \\d 代表[0-9]String s3 = "Hello123";System.out.println(s3.matches(".*\\d.*")?"有数字":"没有");//4.判断字符串是不是标准的手机号String s4 = "13721337351";System.out.println(s4.matches("1[358]\\d{9}") ? "是手机号" : "不是手机号");}
}

. 代表一个符号 * 不能单独出现,代表{0,}

+代表{1,} ?代表{0,1} \d 代表[0-9]

[a-zA-Z]{6,12}

[A-Z]{5}

1[358]\\d{9} \\d 代表[0-9]

\\d{9} 也可以写 [0-9]{9}

\\d* “1242424234”

判断是不是?

判断有没有?

^ $

package cn.practice3;import java.util.Arrays;public class Regexp3 {public static void main(String[] args) {String str = "hello16java16mysql8html";System.out.println(str);//hello16java16mysql8html//删除指定的数字System.out.println(str.replace("6", ""));//hello1java1mysql8html//删除所有数字for (int i = 0; i <= 9; i++) {str = str.replace(String.valueOf(i), "");}System.out.println(str);//hellojavamysqlhtml//------------使用正则表达式,删除所有数字String str2 = "hello16789java16mysql8html";System.out.println(str2);//hello16789java16mysql8html//这两个方法,支持正则表达式 \\d 代表数字   \\D 代表非数字//String.replaceAll()  String.replaceFirst();System.out.println(str2.replaceAll("\\d", ""));//删除字符串数字内容//hellojavamysqlhtmlSystem.out.println(str2.replaceAll("\\D", ""));//删除不是数字的内容//16789168//删除首次出现的字符串匹配删除 \\d+System.out.println(str2.replaceFirst("\\d+", ""));//hellojava16mysql8htmlString[] langs = str2.split("\\d+");System.out.println(Arrays.toString(langs));//[hello, java, mysql, html]}
}

String 支持正则表达式方法

.replaceAll()

.replaceFirst()

.split()

.matches()

String StringBuilder StringBuffer
  • 长度
  • 字节数
  • 搜索 检索位置indexof() lastIndexof()
  • startWith
  • endWith
package cn.practice3;public class Regexp4 {public static void main(String[] args) {//判断字符串有没有大写String str = "hello";//.*[A-Z].*// .代表一个任意符号if (str.matches("^.*[A-Z].*$")) {System.out.println(String.format("%s:有大写字母", str));} else {System.err.println(String.format("%s:没有大写字母", str));}// 判断字符串有没有数字System.out.println("hello".matches("^.*\\d.*$"));//判断有没有汉字System.out.println("china".matches("^.*[\u4e00-\u9fa5].*$"));// 判断是不是手机号String phone = "13014577066";String pattern = "^1[3,5,8]\\d{9}$";if (phone.matches(pattern)) {System.out.println("yes");} else {System.out.printf("手机号:%s不是合法的手机号。 \n", phone);}// 判断是不是纯中文String name = "jack";String p = "^[\u4e00-\u9fa5]+$";if (name.matches(p)) {System.out.println(name);} else {System.out.println("姓名必须为纯中文");}String str1 = "他的手机号是:13014577033,他在郑州。";System.out.println(str1.matches(".*\\d.*"));//判断str是不是标准的手机号格式System.out.println(str1.matches("1[358]\\d{9}"));//判断字符串有没有手机号System.out.println(str1.matches(".*1[358]\\d{9}.*"));//是不是中文是不是一个汉字 ^开头 $代表结尾   {1}System.out.println("中文".matches("^[\u4e00-\u9fa5]$"));//false//+ {,}System.out.println("中文".matches("^[\u4e00-\u9fa5]+$"));//trueSystem.out.println("中文".matches("[\u4e00-\u9fa5]+"));//trueSystem.out.println("--".repeat(20));//* 代表是 {0,}System.out.println("中文a".matches("[\u4e00-\u9fa5]*"));//falseSystem.out.println("".matches("[\u4e00-\u9fa5]*"));//true// ? {0,1}System.out.println("中".matches("[\u4e00-\u9fa5]?"));//trueSystem.out.println("中文".matches("[\u4e00-\u9fa5]{2,6}"));//trueSystem.out.println("**".repeat(25));//是不是全英文System.out.println("abc".matches("[a-zA-Z]+"));//trueSystem.out.println("ab c".matches("[a-zA-Z]+"));//falseSystem.out.println("ab123c".matches("[a-zA-Z]+"));//falseSystem.out.println("abDDDc".matches("[a-zA-Z]{1,}"));//trueSystem.out.println("___".repeat(20));//是不是全数字 "\\d+"  "\\d*"System.out.println("0000".matches("\\d"));//falseSystem.out.println("0000".matches("\\d+"));//trueSystem.out.println("0000".matches("\\d?"));//falseSystem.out.println("0000".matches("\\d*"));//true}
}

java_note9相关推荐

最新文章

  1. create-react-app 2.0中使用antd(eject)
  2. go使用反射reflect获取变量类型
  3. 清华镜像源安装 NGboost XGboost Catboost
  4. PHP生成登录图片验证码
  5. 微信支付curl: (60) SSL certificate problem: unable to get local issuer certificate 解决方法
  6. 递归处理汉诺塔问题(c++/python)
  7. Linux mkdir 与 mkdir -p 的区别
  8. linux内核装载vfs过程
  9. Linux下查看NVIDIA的GPU使用情况
  10. 个性化密码破解字典生成工具:cupp
  11. Js传参中文乱码解决方法
  12. PHP使用Socket发送字节流
  13. 分析锂电池充放电保护电路的特点及工作原理
  14. MVC 简介,MVC数据库
  15. 技能兴鲁试题--可视化
  16. oracle 幻影读,索引+事务
  17. 艾兰岛编辑器-玩家角色
  18. Java基础之String类(第六天)
  19. 高清壁纸软件 FreshBackMac for Mac 1.9.2免费版
  20. 解决deepin更换输入法之后系统报错、出现bug的问题

热门文章

  1. 尚硅谷谷粒音乐项目学习笔记及答疑解惑(1-20集)
  2. php微服务swoole,2.2 swoole
  3. 完美解决Non-terminating decimal expansion; no exact representable decimal result.异常
  4. 关于typescript 报错问题
  5. CentOS7.9搭建NTP服务器
  6. Python 小白的吸星大法 !
  7. 第2章丨IRIS Global 结构
  8. 记录一下复现CLAM
  9. 2023世界人工智能大会 | 智能媒体计算专题论坛
  10. iOS Swift 5中的键盘处理