本文最后更新于 2024年9月22日 晚上
字符串
String对象不可变
(修改字符串的方法实质上是生成了一个新的String对象)
StringBuilder
new StringBuilder(String s) |
生成方法 |
append() |
增加字符 |
deleteCharAt(int index) |
删除指定索引的字符 |
toString() |
生成字符串 |
reverse() |
逆转 |
replace(int start, int end, String str) |
替换 |
delete(int start, int end) |
删除 |
insert(int offset, String str) |
插入 |
1 2 3 4 5 6 7 8 9 10
| public String toString(){ StringBuilder result = new StringBuilder("["); for (int i = 0; i < 10; i++){ result.append(rand.nextInt(100)); result.append(", "); } result.delete(result.length()-2, result.length()); result.append("]"); return result.toString(); }
|
无意识递归
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class InfiniteRecursion{ public String toString(){ return " InfiniteRecursion address: " + this + "\n"; } public static void main(String[] args){ List<InfiniteRecursion> v = new ArrayList<InfiniteRecursion>(); for (int i = 0; i < 10; i++){ v.add(new InfiniteRecursion()); } System.out.println(v); } }
|
方法 |
参数,重载版本 |
应用 |
length() |
|
字符个数 |
charAt() |
Int索引 |
该缩影位置上的char |
getChar(),getBytes() |
|
复制 |
toCharArray() |
|
生成char[] |
equals(),equalsIgnoreCase() |
|
比较字符串 |
compareTo() |
|
按词典序比较字符串 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import static net.mindview.util.Print.print; public class StringMethod { public static void main(String[] args) { String s1 = "good,world", s2="hello,world"; print(s1.regionMatches(5,s2,6,5)); print(s1.startsWith("g")); print(s1.endsWith("g")); print(s1.length()); print(s1.charAt(0)); print(s1.compareTo(s2)); String s3 = "zzz", s4 = "AAA"; print(s1.compareTo(s3)); print(s1.compareToIgnoreCase(s4)); }
}
|
格式化说明符
类型转换字符 |
|
|
|
d |
十进制 |
e |
浮点数(科学计数) |
c |
Unicode字符 |
x |
十六进制 |
b |
Boolean |
h |
散列码 |
s |
String |
% |
% |
f |
浮点数 |
|
|
正则表达式(Regular
Expression)
基础
1 2 3 4 5 6 7 8
| -?查找带负号的数 \\d 表示一位数字 \\\\ 普通反斜线 -?\\d+ (-|\\+)? 一个-或者一个+ \\W 非单词字符 \\w 单词字符 \? 问号
|
字符
B |
指定字符 |
|
十六进制值为oxhh的字符 |
|
十六进制值为oxhhhh的字符 |
Tab |
|
|
换行符 |
回车 |
|
换页 |
|
|
转义 |
字符类
. |
任意字符 |
[abc] |
a|b|c |
[^abc] |
否定 |
[a-zA-Z] |
a-z,A-Z范围 |
[abc[hij]] |
a|b|c|h|i|j |
[a-z&&[hij]] |
h|i|j |
|
空白符 |
|
非空白符 |
数字 |
|
|
非数字 |
|
词字符 |
|
非词字符 |
边界匹配符
^ |
一行的起始 |
$ |
一行的结束 |
词的边界 |
|
|
非词的边界 |
前一个匹配的结束 |
|
量词
- 贪婪型:尽可能多的匹配
- 勉强型:匹配最少的字数
- 占有型:防止回溯,java中才有
贪婪型 |
勉强型 |
占有型 |
符合匹配 |
X? |
X?? |
X?+ |
一个或零个X |
X* |
X*? |
X*+ |
零个或多个X |
X+ |
X+? |
X++ |
一个或多个X |
X{n} |
X{n}? |
X{n}+ |
恰好n次X |
X{n,m} |
X{n,m}? |
X{n,m}+ |
X至少n次,且不超过m次 |
Pattern.compile()
编译正则表达式
Pattern.matcher()
生成Matcher对象
Matcher.matches()
判断整个输入字符串是否匹配正则表达式模式
Matcher.find()/Matcher.find(int
i)
查找(int i可以指定起始位置)
Pattern标记
编译标记 |
效果 |
Pattern.CANON_EQ |
规范的等价性 |
Pattern.CASE_INSENSITIVE(?i) |
大小写不敏感 |
Pattern.COMMENTS(?x) |
忽略空格符,注释 |
Pattern.DOTALL(?x) |
"."匹配所有字符,包括行终结符 |
Pattern.MULTILINE(?m) |
^,\(匹配一行的开始和结束 |
| Pattern.UNICODE_CASE(?u) | 大小写不敏感按照Unicode标准 |
| Pattern.UNIX_LINES(?d) | .,^,\)均只识别行终结符 |
Scanner
1 2 3 4
| Scanner stdin = new Scanner(SimpleRead.input); String name = stdin.nextLine(); int age = stdin.nextInt(); double favourite = stdin.nextDouble();
|
- Scanner的构造器可以接受任何类型的输入对象,包括File对象,InputStream对象,String或Readable对象.
useDelimiter()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import java.util.*;
public class ScannerDelimiter{ public static void main(String[] args){ Scanner scanner = new Scanner("12, 42, 12"); scanner.useDelimiter("\\s*,\\s*"); while (scanner.hasNextInt()){ System.out.println(scanner.nextInt()); } } }
|