黑马程序员--java基础之String类中一些方法的代码原理
练习一:模拟trim方法,去除字符串中的开头结尾空格。
提示:1、String类提供的方法为String trim();
2、需要用的方法有char charAt(int index);返回index索引的字符;
3、用String substring(int beginIndex,int endIndex);
|
1
2
3
4
5
6
7
8
9
10
|
class StringTest{ public static String myTrim(String str){ int begin = 0; int end = str.length()-1; while(begin<=end && str.charAt(begin) == ' ') begin++; while(begin<=end && str.charAt(end) == ' ') end--; return str.substring(start, end+1); } |
练习二:将字符串中的字符反转;
提示:1、java中提供了StringBuffer类,此类中有static String reserse()方法;
2、首先需要将此字符串转换成字符数组,用String类中的char [] = str.toCharArray()方法;
3、将字符数组中的字符反转,用for循环;
4、将得到的字符数组转换成字符串,用new String(char [] chs);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class StringTest{ public static String reverseString(String str){ char [] chs = str.toCharArray(); replace(chs); return new String(chs); } private static void replace(char [] chs){ for(int begin=0, end=chs.length-1; begin<end; begin++,end--) swap(chs,begin,end); } private static void swap(char [] chs,int x,int y){ char temp = chs[x]; chs[x] = chs[y]; chs[y] = temp; } |
练习二的优化:
目的:可以随意反转字符串中的任意一段字符串;
提示:重载reverseString()方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class StringTest{ public static String reverseString(String str,int begin, int end){ char [] chs = str.toCharArray(); replace(chs,begin,end); return new String(chs); } public static String reverseString(String str){ return reverseString(str, 0, str.length()); } private static void replace(char [] chs,int begin, int end){ for(int x=begin, y=end-1; x<y; x++,y--) swap(chs,x,y); } private static void swap(char [] chs,int x,int y){ char temp = chs[x]; chs[x] = chs[y]; chs[y] = temp; }} |
练习三:从指定字符串中寻找子字符串出现的次数,例:abccdhccjkjlcclkjkcc寻找cc出现的次数;
提示:1、用到String类中的int str.indexOf(str1,index)方法;
2、while循环的条件是(index=str.indexOf(str1,index))!=-1;
3、循环内部对index重新赋值为index+str2.length();
|
1
2
3
4
5
6
7
8
9
10
11
12
|
class StringTest{ public static int indexOfString(String str, String str1){ int index = 0; int count = 0; while((index=str.indexOf(str1,index)) != -1){ System.out.println(index); index = index + str1.length(); count++; } return count; }} |
练习四:获取字符串中最大的子串,例:"adsfadhellofasdf" "ddhelloff"
提示:1、首先判断传入的俩个字符串哪个是大的哪个是小的;
2、利用大圈套小圈方法循环,外层控制每次减少的字符数,从0到小字符串的长度; 内层控制小字符串的比较次数,需要定义倆个变量,一个是控制字符串开始比较的位置,从0开始,另一个是控制字符串结束比较的位 置;条件是第二个变量的值不等于小字符串的长度
3、用String类的String str1.substring(y,z)方法获取到剪掉后的字符串;
再用String类的boolean str.contains(temp)判断是否相同
|
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
|
class StringTest{ public static String getMaxString(String str, String str1){ String max = str.length() > str1.length() ? str : str1; String min = (max == str) ? str1 : str; for(int i=0; i<min.length(); i++){ for(int y=0, z=min.length()-i; z!=min.length()+1; y++,z++){ String temp = min.substring(y,z); if(max.contains(temp)) return temp; } } return " "; //System.out.println(temp); }*/ public static void main(String [] args){ //String str = " abd dk "; String str = "adsfadhellofasdf"; String str1 = "ddhelloff"; str = myTrim(str); System.out.println(getMaxString(str,str1)); //System.out.println(reverseString(str)); //System.out.println(indexOfString(str,"cc")); }} |

浙公网安备 33010602011771号