Java高级字符串处理

1、正则表达式
2、其他字符串操作
-集合和字符串操作
-字符串转义
-变量名字格式化
-从字符串到输入流

正则表达式
1、规则表达式,计算机科学的一个基础概念
2、用事先定义好的一些特定字符及这些特定字符的组合,组成一个“规则字符串“,
3、作用
-测试字符串内的模式
-识别/替换文本
-提取文本
4、正则表达式独立于特定语言
5、正则表达式的匹配模板
-定界符
-原子
-特殊功能
-模式修正符
1、Pattern正则表达式的编译表示
-compile编译一个正则表达式为Pattern对象
-matcher用Pattern对象匹配一个字符串,返回匹配结果

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Person2{
private static final String regex = “\bdog\b”; //\b表示边界
private static final String input = “dog dog doggie dogg”;
public static void main(String[] args) throws Exception{
//检查字符串里有多少个dog
Pattern p = Pattern.compile(regex);//把正则表达式的字符串编译成正则表达式的对象
Matcher m = p.matcher(input);
int count = 0;
while(m.find()) {
count++;
System.out.println(“number”+count);//计数
System.out.println(“start”+m.start());//从何处开始
System.out.println(“end”+m.end());//从何处结束
}
}
}


public class Person2{
private static final String regex = “foo”;
private static final String input = “foooooooooo”;
private static Pattern pattern;
private static Matcher matcher;
public static void main(String[] args) {
//检查字符串里有多少个dog
pattern = Pattern.compile(regex);
matcher = pattern.matcher(input);
System.out.println(“regerx”+regex); //foo
System.out.println(“input”+input); // foooooooooo
System.out.println("looking "+matcher.lookingAt());//部分匹配 looking true
System.out.println("match "+matcher.matches());//完全匹配 match false
}
}

public class Person2{
private static final String regex = “a*b”;//*前面可以有0或者多个a
private static final String input = “aabfooabfoobfoo”;
private static String replace = “-”;
public static void main(String[] args) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
StringBuffer sb = new StringBuffer();
//全部替换
while(m.find()) {
m.appendReplacement(sb, replace);
}
//将最后的尾部字符串加上
m.appendTail(sb);
System.out.println(sb.toString());//-foo-foo-foo
}
}

public class Person2{
private static String regex = “dog”;
private static String input = “the dog says meow,all dogs say meow”;
private static String replace = “cat”;
public static void main(String[] args) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
input = m.replaceAll(replace); //把所以的dog替换成cat
System.out.println(input); //the cat says meow,all cats say meow
}
}

字符串和集合互转
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class Person2{
public static void main(String[] args) {
List names = new LinkedList();
names.add(“jack”);
names.add(“marry”);
names.add(“tom”);
//从list变到字符串
String str1 = String.join(",", names);
System.out.println(str1); //jack,marry,tom
String str2 = StringUtils.join(names,",");//引入Apache common lang包
//从字符串变成list
List names2 = Arrays.asList(str2.split(","));
for(String name:names2) {
System.out.println(name); // jack marry tom
}
}
}

字符转义
引入Apache common lang包
public class Person2{
public static void main(String[] args) {
String str = “he didn’t say,“Stop!””;
//转义
String escape = StringEscapeUtils.escapeJava(str);
System.out.println(“escape”+escape); //escape he didn’t say,“Stop!”
//从转义字符转回
String str2 = StringEscapeUtils.unescapeJava(secapeStr);
System.out.println(“unescape”+str2); //unescape he didn’t say,“Stop!”
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值