ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

[20-05-26][Thinking in Java 45]Java String 3 - Replace

2020-05-26 23:56:02  阅读:216  来源: 互联网

标签:26 Java String Beware System sbuf println out


 1 package test_21_3;
 2 
 3 import java.util.regex.Matcher;
 4 import java.util.regex.Pattern;
 5 
 6 public class ReplaceString {
 7 
 8     public static void main(String[] args) {
 9         
10         String str = "Twas brillig, and the slithy toves\n" +
11                  "Did gyre and gimble in the wabe.\n" + 
12                  "All mimsy were the borogoves,\n" + 
13                  "Beware the Jabberwock, my son,\n" +
14                  "The jaws that bite, the claws that catch.\n" + 
15                  "Beware the Jubjub bird, and shun\n" + 
16                  "The frumious Bandersnatch.";
17         
18         String regex1 = "\\bb\\w+\\b";
19         // 替换第一个匹配项
20         String srf = str.replaceFirst(regex1, "[ReplaceFirst]");
21         
22         System.out.println(srf);
23         System.out.println("---------------");
24         
25         String regex2 = "\\ba\\w+\\b";
26         // 替换所有匹配项
27         String sra = str.replaceAll(regex2, "[ReplaceAll]");
28         
29         System.out.println(sra);
30         System.out.println("----------------");
31         
32         StringBuffer sbuf = new StringBuffer();
33         String regex3 = "[aeiou]";
34         Pattern p = Pattern.compile(regex3);
35         Matcher m = p.matcher(str);
36         
37         // 替换前15个匹配项
38         for (int i = 0; i < 15; i++) {
39             m.find();
40             m.appendReplacement(sbuf, m.group().toUpperCase());
41         }
42         // 将剩下的部分复制进sbuf
43         m.appendTail(sbuf);
44         
45         System.out.println(sbuf);
46                 
47     }
48 }

 

结果如下:

Twas [ReplaceFirst], and the slithy toves
Did gyre and gimble in the wabe.
All mimsy were the borogoves,
Beware the Jabberwock, my son,
The jaws that bite, the claws that catch.
Beware the Jubjub bird, and shun
The frumious Bandersnatch.
---------------
Twas brillig, [ReplaceAll] the slithy toves
Did gyre [ReplaceAll] gimble in the wabe.
All mimsy were the borogoves,
Beware the Jabberwock, my son,
The jaws that bite, the claws that catch.
Beware the Jubjub bird, [ReplaceAll] shun
The frumious Bandersnatch.
----------------
TwAs brIllIg, And thE slIthy tOvEs
DId gyrE And gImblE In thE wabe.
All mimsy were the borogoves,
Beware the Jabberwock, my son,
The jaws that bite, the claws that catch.
Beware the Jubjub bird, and shun
The frumious Bandersnatch.

标签:26,Java,String,Beware,System,sbuf,println,out
来源: https://www.cnblogs.com/mirai3usi9/p/12969645.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有