halisi7

一个专注技术的组织

0%

Java-常用类-字符串练习

字符串练习

练习一:

image-20220128100703600

代码:

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
public class StringDemo {

/*
将一个字符串进行反转。将字符串中指定部分进行反转。比如“abcdefg”反转为”abfedcg”

方式一:转换为char[]
*/
public String reverse(String str,int startIndex,int endIndex){

if(str != null){
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;
}

//方式二:使用String的拼接
public String reverse1(String str,int startIndex,int endIndex){
if(str != null){
//第1部分
String reverseStr = str.substring(0,startIndex);
//第2部分
for(int i = endIndex;i >= startIndex;i--){
reverseStr += str.charAt(i);
}
//第3部分
reverseStr += str.substring(endIndex + 1);

return reverseStr;

}
return null;
}
//方式三:使用StringBuffer/StringBuilder替换String
public String reverse2(String str,int startIndex,int endIndex){
if(str != null){
StringBuilder builder = new StringBuilder(str.length());

//第1部分
builder.append(str.substring(0,startIndex));
//第2部分
for(int i = endIndex;i >= startIndex;i--){

builder.append(str.charAt(i));
}
//第3部分
builder.append(str.substring(endIndex + 1));

return builder.toString();
}
return null;

}

@Test
public void testReverse(){
String str = "abcdefg";
String reverse = reverse2(str, 2, 5);
System.out.println(reverse);
}

}

练习2:

image-20220128100834802

代码:

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
public class StringDemo1 {
/*
获取一个字符串在另一个字符串中出现的次数。
比如:获取“ab”在 “abkkcadkabkebfkaabkskab” 中出现的次数

*/

/**
* 获取subStr在mainStr中出现的次数
* @param mainStr
* @param subStr
* @return
*/
public int getCount(String mainStr,String subStr){
int mainLength = mainStr.length();
int subLength = subStr.length();
int count = 0;
int index = 0;
if(mainLength >= subLength){
//方式一:
// while((index = mainStr.indexOf(subStr)) != -1){
// count++;
// mainStr = mainStr.substring(index + subStr.length());
// }
//方式二:对方式一的改进
while((index = mainStr.indexOf(subStr,index)) != -1){
count++;
index += subLength;
}

return count;
}else{
return 0;
}
}

@Test
public void testGetCount(){
String mainStr = "abkkcadkabkebfkaabkskab";
String subStr = "ab";
int count = getCount(mainStr, subStr);
System.out.println(count);
}
}

练习3:

image-20220128101922036

代码:

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
public class StringDemo2 {
/*
获取两个字符串中最大相同子串。比如:
str1 = "abcwerthelloyuiodefabcdef";str2 = "cvhellobnm"
提示:将短的那个串进行长度依次递减的子串与较长的串比较。

*/
//前提:两个字符串中只有一个最大相同子串
public String getMaxSameString(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;
int length = minStr.length();

for(int i = 0;i < length;i++){
for(int x = 0,y = length - i;y <= length;x++,y++){
String subStr = minStr.substring(x,y);
if(maxStr.contains(subStr)){
return subStr;
}

}
}

}
return null;
}

// 如果存在多个长度相同的最大相同子串
// 此时先返回String[],后面可以用集合中的ArrayList替换,较方便
public String[] getMaxSameString1(String str1, String str2) {
if (str1 != null && str2 != null) {
StringBuffer sBuffer = new StringBuffer();
String maxString = (str1.length() > str2.length()) ? str1 : str2;
String minString = (str1.length() > str2.length()) ? str2 : str1;

int len = minString.length();
for (int i = 0; i < len; i++) {
for (int x = 0, y = len - i; y <= len; x++, y++) {
String subString = minString.substring(x, y);
if (maxString.contains(subString)) {
sBuffer.append(subString + ",");
}
}
// System.out.println(sBuffer);
if (sBuffer.length() != 0) {
break;
}
}
String[] split = sBuffer.toString().replaceAll(",$", "").split("\\,");
return split;
}

return null;
}

@Test
public void testGetMaxSameString(){
String str1 = "abcwerthello1yuiodefabcdef";
String str2 = "cvhello1bnmabcdef";
String[] maxSameStrings = getMaxSameString1(str1, str2);
System.out.println(Arrays.toString(maxSameStrings));

}

}
打赏一下作者~ ฅ( ̳• ◡ • ̳)ฅ