深入Java之String

String
终于忙完了学校的事情,接下来就是要好好准备面试了,如题,走起……

第一阶段

这个阶段主要是了解String的主要使用方法并能够熟练使用。此阶段主要是多动手,多思考。
下面是这一阶段的总结:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package string;

/*
* String类是对字符串的封装。
* 那么它就提供了对字符串进行操作的方法。
*
* 常见的操作有哪些?
* 1. 获取
* 1.1 字符串的长度。
* int length()
* 1.2 根据位置获取位置上的某个字符(从0开始),如果不存在则出现StringIndexOutOfBoundsException。
* char charAt(int index)
* 1.3 根据字符获取该字符在字符串中的位置(从0开始)。
* int indexOf(int ch):返回ch在字符串中第一次出现的位置,如果不存在则返回-1
* int indexOf(int ch, int fromIndex):从fromIndex开始,获取ch在字符串中出现的位置
*
* int indexOf(String str):返回str在字符串中第一次出现的位置
* int indexOf(String str, int fromIndex):从fromIndex开始,获取str在字符串中出现的位置
*
* int lastIndexOf(int ch)
* int lastIndexOf(int ch, int fromIndex)
* int lastIndexOf(String str)
* int lastIndexOf(String str, int fromIndex)
* 1.4 获取子串
* CharSequence subSequence(int beginIndex, int endIndex):返回一个新的字符序列,它是此序列的一个子序列。
* String substring(int beginIndex):返回一个新的字符串,它是此字符串的一个子字符串。
* String substring(int beginIndex, int endIndex):返回一个新字符串,它是此字符串的一个子字符串,包含头,不包含尾。
*
* 2. 判断
* 2.1 字符串是否为空
* boolean isEmpty():原理就是判断该字符串的length()是否为0
*
* 2.2 字符串是否包含一个子串
* boolean contains(CharSequence s)
* * indexOf(str)可以索引子串第一次出现的位置,如果不存在则返回-1,所以也可以判断是否包含
*
* * public interface CharSequence
* * 所有已知实现类: CharBuffer, Segment, String, StringBuffer, StringBuilder
* * CharSequence 是 char 值的一个可读序列。此接口对许多不同种类的 char 序列提供统一的只读访问。char 值表示 Basic Multilingual Plane (BMP) 或代理项中的一个字符。有关详细信息,请参阅 Unicode 字符表示形式。
* * 此接口不修改 equals 和 hashCode 方法的常规协定。因此,通常未定义比较实现 CharSequence 的两个对象的结果。每个对象都可以通过一个不同的类实现,而且不能保证每个类能够测试其实例与其他类的实例的相等性。
* * 因此,使用任意 CharSequence 实例作为集合中的元素或映射中的键是不合适的。

* 2.3 字符串是否以某个子串开头
* boolean startsWith(String str)
* 2.4 字符串是否以某个子串结尾
* boolean endsWith(String str)
*
* 2.5 判断字符串的内容是否相同,复写了Object中的equals方法
* boolean equals(Object obj):将此字符串与指定的对象比较。当且仅当该参数不为 null,并且是与此对象表示相同字符序列的 String 对象时,结果才为 true。
* boolean equals(CharSequence cs):将此字符串与指定的 CharSequence 比较。当且仅当此 String 与指定序列表示相同的 char 值序列时,结果才为 true。
* boolean equalsIgnoreCase(String str):忽略大小写进行内容比较
*
* 2.6 判断字符串是否符合给定的规则
* boolean matches(String regex):告知此字符串是否匹配给定的正则表达式。
*
* 2.7 对两个字符串进行比较
* int compareTo(String anotherString):按字典顺序比较两个字符串。主要用于String排序,更多: http://blog.csdn.net/scyatcs/article/details/16974893
* int compareToIgnoreCase(String str):按字典顺序比较两个字符串,不考虑大小写。
*
* 3. 转换
* 3.1 将字符数组转换成字符串
* 构造函数: String(char[] value)
* String(char[] value, int offset, int count):将字符数组的一部分转换成字符串
*
* 静态方法: static copyValueOf(char[] data)
* static copyValueOf(char[] data, int offset, int count)
*
* static valueOf(char[] data)
* static valueOf(char[] data, int offset, int count)
* 3.2 将字符串转成字符数组
* char[] toCharArray()
*
* 3.3 将字节数组转换成字符串
* String(byte[] bytes)
* String(byte[] bytes, Charset charset)
* String(byte[] bytes, String charsetName)
*
* String(byte[] bytes, int offset, int length)
* String(byte[] bytes, int offset, int length, Charset charset)
* String(byte[] bytes, int offset, int length, String charsetName)
*
* 3.4 将字符串转换成字节数组
* byte[] getBytes()
* byte[] getBytes(Charset charset)
* byte[] getBytes(String charsetName)
*
* 3.5 将基本数据类型转换成字符串
* static String valueOf(boolean b)
* static String valueOf(char c)
* static String valueOf(double d)
* static String valueOf(float f)
* static String valueOf(int i) //3+"";String.valueOf(3);
* static String valueOf(long l)
* static String valueOf(Object obj) //由obj的toString方法实现
*
* 3.6 分割字符串成字符串数组
* String[] split(String regex):根据给定正则表达式的匹配拆分此字符串。
* String[] split(String regex, int limit):根据匹配给定的正则表达式来拆分此字符串。
* 例如,字符串 "boo:and:foo" 使用这些参数可生成以下结果:
* Regex Limit 结果
* : 2 { "boo", "and:foo" }
* : 5 { "boo", "and", "foo" }
* : -2 { "boo", "and", "foo" }
* o 5 { "b", "", ":and:f", "", "" }
* o -2 { "b", "", ":and:f", "", "" }
* o 0 { "b", "", ":and:f" }
*
* 3.7 转换大小写
* String toLowerCase():使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
* String toLowerCase(Locale locale):使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。
* String toUpperCase()
* String toUpperCase(Locale locale)
*
* 3.8 去除空格
* String trim():返回字符串的副本,忽略前导空白和尾部空白。
*
* 3.9 格式化
* static String format(Locale l, String format, Object... args):使用指定的语言环境、格式字符串和参数返回一个格式化字符串。
* static String format(String format, Object... args):使用指定的格式字符串和参数返回一个格式化字符串。
* 更多关于format()的使用:http://blog.csdn.net/lonely_fireworks/article/details/7962171/
*
* 4. 替换
* String replace(char oldChar, char newChar)
* 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
* String replace(CharSequence target, CharSequence replacement)
* 使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
* String replaceAll(String regex, String replacement)
* 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
* String replaceFirst(String regex, String replacement)
* 使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。
*
*/

public class StringMethodDemo {
public static void method_replace() {
String s = "Hello Java";
String s1 = s.replace('a', 'b');//如果要替换的字符不存在则返回原字符串

sop("s="+s);//字符串一旦被初始化就不会改变
sop("s1="+s1);
}

public static void method_trans() {
char[] arr = {'a','b','c','d','e','f','g'};
String str = new String(arr,1,3);//bcd,1为下标(从0开始),3为个数
sop("str="+str);

char[] chs = str.toCharArray();
for(char c : chs) {
sop(c);
}
}

public static void method_is() {
String str = "ArrayDemo.java";

//判断文件名称是否是以"Array"开头
sop(str.startsWith("Array"));
//判断文件类型是否是java文件
sop(str.endsWith(".java"));
//判断文件名是否包含"Demo"
sop(str.contains("Demo"));

//判断内容是否相同
sop(str.equals(new StringBuilder("ArrayDemo.java")));//参数必须为String类型,false
sop(str.contentEquals(new StringBuilder("ArrayDemo.java")));//参数为CharSequence接口,true
}

public static void method_get() {
String str = "abcdefabss";

//长度
sop(str.length());

//根据索引获取字符
sop(str.charAt(4));//当访问到字符串中的下标不存在时会出现StringIndexOutOfBoundsException

//根据字符获取索引
sop(str.indexOf('a'));
sop(str.indexOf('a',3));//如果没有找到则返回-1

//反向索引一个字符出现的位置,返回该字符在String中的下标。
sop(str.lastIndexOf('a'));

//子串
sop(str.substring(2));
sop(str.substring(2,4));
}

public static void main(String[] args) {
// method_get();
// method_is();
// sop(reverse(str));
// sop(reverse(str,2,6));
sop(getMaxSubString("dsalkjflksdfj","rrueuiorsaxcmvm"));
}

public static void sop(Object obj) {
System.out.println(obj);
}

/*
* 模拟一个trim方法,去除字符串两端的空格。
* 思路:
* 1.获取字符串第一个不为空格字符的下标(分别从左到右和从右到左)
* 2.将左右两边第一个不为字符的下标的中间部分截取出来就是trim后的字符串
*
*/

public static String myTrim(String str) {
int start=0;
int end=str.length()-1;
while(start<=end && str.charAt(start) == ' ')
start++;
while(start<=end && str.charAt(end) == ' ')
end--;

return str.substring(start,end+1);
}

/*
* 将一个字符串进行反转。
* 思路:
* 1.将字符串转换成字符数组
* 2.将字符数组进行反转
* 3.将反转后的字符数组转换成字符串
*/

public static String reverse(String str) {
//方法一
// StringBuilder sb = new StringBuilder(str);
// sb.reverse();
// return sb.toString();
//方法二
char[] chs = str.toCharArray();
for(int start=0,end=chs.length-1; start < end; start++,end--)
swap(chs,start,end);

return String.valueOf(chs);
}

public static void swap(char[] chs, int start, int end) {
char temp = chs[start];
chs[start] = chs[end];
chs[end] = temp;
}

//将字符串中指定部分进行反转。
public static String reverse(String str, int start, int end) {
char[] chs = str.toCharArray();
for(int left=start,right=end-1; left<right; left++,right--)
swap(chs,left,right);

return String.copyValueOf(chs);
}

/*
* 获取一个字符串在另一个字符串中出现的次数
*/

public static int subCount(String str,String key) {
int count = 0;
int index = 0;
while((index=str.indexOf(key,index))!=-1) {
count++;
index+=key.length();
}
return count;
}

/*
* 获取两个字符串中最大相同子串
* 思路:
* 1.将短的字符串依次缩减在长的字符串中进行匹配
*/

public static String getMaxSubString(String s1, String s2) {
String max = s1.length()>s2.length()?s1:s2;
String min = max==s1?s2:s1;
for(int x=0; x<min.length(); x++)
for(int y=0,z=min.length()-x; z<=min.length(); y++,z++) {
String temp = min.substring(y, z);
if(max.contains(temp))
return temp;
}

return "";
}
}

第二阶段

对String的深入理解,从JVM的角度或更高的角度去审视String对象的原理与实现。从网上看到了一篇写的很好的博客:深入理解Java:String

第三阶段

通过一些面试题来检测自己的掌握情况,这部分以后再来添加

坚持原创技术分享,您的支持将鼓励我继续创作