
子表达式 “[ ]” 匹配括号中指定的所有字符。因此,要将所有大写字母移动到字符串的末尾,需要执行以下步骤:
遍历给定字符串中的所有字符。
使用正则表达式 "[A-Z]" 匹配给定字符串中的所有大写字母。
将特殊字符和剩余字符连接到两个不同的字符串中。
-
最后,将特殊字符字符串连接到另一个字符串中。
Java注释规范Skills下载当用户要求为 Java 代码添加注释、修改注释、审查注释、整理注释格式,或要求按照代码注释规范处理 Java 文件时,使用本技能。本技能用于统一生成和维护 Java 类注释模板、按 Git 对比 release 判断版本号、补充每行代码上方中文块注释、调整注释空行规则,并保持 Java 文件 UTF-8 无 BOM 编码。典型触发场景包括:补充类注释、更新 @version、检查 @author 来源、整理行尾注释、把注释放到代码上一行、检查注释是否符合规范、构建前检查 Java 注释风格。
示例 1
public class RemovingSpecialCharacters {
public static void main(String args[]) {
String input = "sample B text C with G upper case LM characters in between";
String regex = "[A-Z]";
String specialChars = "";
String inputData = "";
for(int i=0; i<h2>输出</h2><pre class="brush:php;toolbar:false;">Result: sample text with upper case characters in betweenBCGLM
示例 2
下面是使用 Regex 包的方法将字符串的大写字母移动到末尾的 Java 程序。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String args[]) {
String input = "sample B text C with G upper case LM characters in between";
String regex = "[A-Z]";
String specialChars = "";
System.out.println("Input string: \n"+input);
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(input);
//Creating an empty string buffer
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
specialChars = specialChars+matcher.group();
matcher.appendReplacement(sb, "");
}
matcher.appendTail(sb);
System.out.println("Result: \n"+ sb.toString()+specialChars );
}
}
输出
Input string: sample B text C with G upper case LM characters in between Result: sample text with upper case characters in betweenBCGLM
Java免费学习笔记:立即使用
解锁 Java 大师之旅:从入门到精通的终极指南










