字符串操作和正则表达式
来源:互联网 发布:mysql union all sum 编辑:程序博客网 时间:2024/06/05 00:33
一:字符串基本操作
String s1="ac"; //不创建对立对象的新建方式
byte bys[]=new byte[]{'e','y','l'};
String s2=new String(bys,0,bys.length); //通过数组新建对象
System.out.println(s1+s2);
String s1="abcdefghijklm ";
System.out.println(s1.indexOf('c'));//字符串查找
System.out.println(s1.charAt(5)); //指定位置的字符
System.out.println(s1.substring(6));//从指定位置截取
System.out.println(s1.compareToIgnoreCase("o")); //字符串比较
System.out.println(s1.trim()); //字符串去空格
System.out.println(s1.concat("mq")); //字符串连接
二:字符串的格式化String s1="abcdefghijklm ";
System.out.println(s1.replace("e", "ll")); //字符串替换操作
System.out.println(s1.endsWith("l")); //判断结尾
System.out.println(s1.equals("kklslsk")); //判断是否相等
int a=s1.compareTo("ll");
System.out.println(a); //比较字符串的大小,Unicode
System.out.println(s1.toUpperCase());
Date d1=new Date();
String s1=String.format("%te", d1); //这个月几号?
System.out.println(s1);
String s2=String.format("%tY",d1); //今年是哪一年
System.out.println(s2);
System.out.println(String.format("%tQ", d1)); //绝对毫秒数
三:正则表达式
常用的正则表达式字符正规使用方法
简写方法Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcher("33443");
System.out.println( m.matches());
应用方法String s2="woshibaokaiaaaa@qq.com";
boolean b1= s2.matches("\\w+@\\w+.[a-z]+");
System.out.println(b1);
举例String str="我是一个喜欢游泳,喜欢:唱歌,喜欢运动.喜欢看书的人";
Pattern p=Pattern.compile("\\p{Punct}"); //使用Pattern类定义一个新的模板
String[] s2=p.split(str);
for (String string : s2) {
System.out.println(string+"*");
}
1,验证电话号码 010-53517230
Scanner sa=new Scanner(System.in);
System.out.println("请输入电话号码");
String ex=sa.nextLine();
if(ex.matches("\\d{3,4}-\\d{7,8}")){
System.out.println("符合");
}
else {
System.out.println("不符合");
}
sa.close();2,验证手机号码
System.out.println("请输入手机号码");
String phone=sa.nextLine();
boolean flag=phone.matches("1[3-8]\\d{9}");
if (flag){
System.out.println("格式匹配");
}else {
System.out.println("格式不匹配");
}3、验证用户名,只能以字母开头,字母或者下划线的组合
String username=sa.nextLine();
if(username.matches("[a-zA-Z]+[\\w|_]+")){
System.out.println("匹配");
}else{
System.out.println("不匹配");
}
- 字符串操作和正则表达式
- PHP学习:字符串操作和正则表达式
- PHP学习:字符串操作和正则表达式
- PHP字符串操作和正则表达式
- 正则表达式,字符串操作
- 正则表达式和字符串
- 正则表达式--操作字符串a
- 字符串操作-正则表达式
- 正则表达式---简化字符串操作
- 正则表达式(操作字符串)
- 字符串操作正则表达式方法
- Python练习代码 -- 字符串和正则表达式, 文件文件夹操作
- php 学习笔记 字符串操作和正则表达式(四)
- 字符串和正则表达式-php
- 正则表达式和字符串处理
- 正则表达式和字符串处理
- 正则表达式和字符串处理
- 正则表达式和字符串处理
- 图像处理------基于阈值模糊
- GRE写作必备句型
- jsp表单验证汇总
- Query By Criteria的限制条件写法
- Android 5.1 Lollipop Phone工作流程浅析(十三)__InCallActivity启动Performance浅析
- 字符串操作和正则表达式
- HTML <form> 标签
- 图像处理------调整亮度与饱和度
- JDK安装和Eclipse介绍
- 剑指Offer-->从上向下打印二叉树(★★)
- UVa 11401 Triangle Counting 数三角形
- Java基础语法(一)
- 怎样远程开机内网电脑?
- 【第二步】编程语言的学习