- 问题一:将一个字符串按指定方式反序,比如将“abcdefg”转化为“abfedcg”
- 问题二:获取一个字符串在另外一个字符串中出现的次数,比如:字符串“ab”在字符串“abkkkkaooooabjjadjjb”出现的次数
- 问题三:获取2个字符串中最大的字串。比如str1=”ssnisbihnihaojpo”,str2=”janihaojj”,返回“nihaoj”
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//方式一:转化为char
public String reserve(String str, int startIndex, int endIndex) {
if (str != null && str.length() != 0) {
char[] arr = str.toCharArray();
for (int x = startIndex, y = endIndex; x < y; x++, y--) {
char temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
return new String(arr);
}
return null;
}
public void testReserve() {
String str = "abcdefg";
String reserve1 = reserve2(str, 2, 5);
System.out.println(reserve1);
}
//方式二:使用String的拼接
public String reserve1(String str, int startIndex, int endIndex) { //startIndex为2,即c;endIndex为5,即f
if (str != null && str.length() != 0) {
String strStart = str.substring(0, startIndex);
for (int i = endIndex; i >= startIndex; i--) {
strStart += str.charAt(i);
}
strStart += str.charAt(endIndex + 1);
return strStart;
}
return null;
}
//方式三:使用Builder或Buffer替换
public String reserve2(String str, int startIndex, int endIndex) {
StringBuilder builder = new StringBuilder(str.length());
builder.append(str.substring(0, 2));
for (int i = endIndex; i >= startIndex; i--) {
builder.append(str.charAt(i));
}
builder.append(str.charAt(endIndex + 1));
return builder.toString(); //注意不能返回builder,应该使用toString()方法,转化为String
}
/**
* 获取字符串出现次数的方法
*
* @param mainstr
* @param substr
* @return
*/
public int getcount(String mainstr, String substr) {
int mainLen = mainstr.length();
int subLen = substr.length();
int index = 0;
int count = 0;
if (mainLen > subLen) {
//方式一:每次循环改变index以及mainstr
while ((index = mainstr.indexOf(substr)) != -1) {
count++;
mainstr = mainstr.substring(index + substr.length());
}
//方式二:每次循环只改变index,mainstr始终不变
while ((index = mainstr.indexOf(substr, index)) != -1) {
count++;
index += subLen;
}
return count;
} else {
return 0;
}
}
public void testGet() {
String mainstr = "abkkkkaboooabjjadjjb";
String substr = "ab";
int getcount = getcount(mainstr, substr);
System.out.println(getcount);
}
/**
*获取2个字符串中最大的字串
* @param str1
* @param str2
* @return
*/
public String getStr(String str1, String str2) {
if (str1 != null && str2 != null) {
String maxstr = (str1.length() >= str2.length()) ? str1 : str2;
String minstr = (str1.length() < str2.length()) ? str1 : str2;
for (int i = 0; i < minstr.length(); i++) {//第一大轮:扣出去0个;第二大轮:扣出去1个。。。
//2个指针,一个在头,一个在尾
for (int j = 0, k = minstr.length() - i; k <= minstr.length(); k++, j++) {//比如:扣1个,先j-->k-1 if没有 再j+1--k if没有 k++会大于minstr.length跳出循环
//扣2个,先j-->k-2 if没有 再j+1-->k-1 if没有 再j+2-->k if没有 k++会大于minstr.length跳出循环
String substr = minstr.substring(j, k);
if (maxstr.contains(substr)) {
return substr;
}
}
}
}
return null;
}
public void testGetStr() {
String str1 = "ssnisbihnihaojpo";
String str2 = "janihaojj";
String str = getStr(str1, str2);
System.out.println(str);
}