public class RegexTest {
public static void main(String[] args) {
test_1();
}
/*
* 需求:将以下字符串转成:我要学编程。
*
* 到底用四种功能中的哪一种呢?或者哪几个呢?
* 思路方法:
* 1,如果只想知道该字符串是对是错,使用匹配。
* 2,想要将已有的字符串变成另一个字符串,替换。
* 3,想要按照指定的方式将字符串变成多个字符串。切割。
* 4,想要拿到符合需求的字符串子串,获取。获取符合规则的子串。
*/
public static void test_1(){
String str = "我我...我我...我要..要...要要...学学学...学学...编编编...编编..程.程...程...程";
/*
* 将已有字符串变成另一个字符串。使用 替换功能。
* 1,可以先将 . 去掉。
* 2,再将多个重复的内容变成单个内容。
*/
str = str.replaceAll("\\.+", "");
System.out.println(str);
str = str.replaceAll("(.)\\1+","$1");
System.out.println(str);
}
}