java代码中实现英文字符括号、中括号、斜杠、反斜杠、句号、逗号、分号、冒号、单引号、双引号、叹号、省略号转换
时间: 2023-12-01 16:04:49 浏览: 253
以下是Java代码中实现英文字符括号、中括号、斜杠、反斜杠、句号、逗号、分号、冒号、单引号、双引号、叹号、省略号转换的方法:
1. 英文字符括号、中括号、斜杠、反斜杠、句号、逗号、分号、冒号、单引号、双引号、叹号、省略号转义
```java
String str = "英文字符括号、中括号、斜杠、反斜杠、句号、逗号、分号、冒号、单引号、双引号、叹号、省略号";
str = str.replaceAll("\\(", "\\\\(")
.replaceAll("\\)", "\\\\)")
.replaceAll("\\[", "\\\\[")
.replaceAll("\\]", "\\\\]")
.replaceAll("\\\\", "\\\\\\\\")
.replaceAll("\\.", "\\\\.")
.replaceAll(",", "\\\\,")
.replaceAll(";", "\\\\;")
.replaceAll(":", "\\\\:")
.replaceAll("'", "\\\\'")
.replaceAll("\"", "\\\\\"")
.replaceAll("!", "\\\\!")
.replaceAll("\\.\\.\\.", "\\\\...");
System.out.println(str);
```
2. 英文字符括号、中括号、斜杠、反斜杠、句号、逗号、分号、冒号、单引号、双引号、叹号、省略号反转义
```java
String str = "英文字符括号、中括号、斜杠、反斜杠、句号、逗号、分号、冒号、单引号、双引号、叹号、省略号";
str = str.replaceAll("\\\\\\(", "\\(")
.replaceAll("\\\\\\)", "\\)")
.replaceAll("\\\\\\[", "\\[")
.replaceAll("\\\\\\]", "\\]")
.replaceAll("\\\\\\\\", "\\\\")
.replaceAll("\\\\\\.", "\\.")
.replaceAll("\\\\\\,", ",")
.replaceAll("\\\\\\;", ";")
.replaceAll("\\\\\\:", ":")
.replaceAll("\\\\'", "'")
.replaceAll("\\\\\"", "\"")
.replaceAll("\\\\\\!", "!")
.replaceAll("\\\\\\.\\\\\\.\\\\\\.", "\\.\\.\\.");
System.out.println(str);
```
阅读全文