一,模拟一个trim方法,去除字符串两端的空格。
思路:1,判断字符串第一个位置是否是空格,如果是继续向下判断,直到不是空格为止。
结尾处判断空格也是如此。
2,当开始和结尾都判断到不是空格时,就是要获取的字符串。
二,将一个字符串进行反转。
将字符串中指定部分进行反转,"abcdefg";abfedcg
思路:
1,曾经学习过对数组的元素进行反转。
2,将字符串变成数组,对数组反转。
3,将反转后的数组变成字符串。
4,只要将或反转的部分的开始和结束位置作为参数传递即可。
class StringTest
{
public static void sop(String str)
{
System.out.println(str);
}
public static void main(String[] args)
{
String s = " ab8 ef6 ";
sop("Orig:"+s);
// s = myTrim(s);
// sop(s);
sop("All :"+reverseString(s));
sop("Part:"+reverseString(s,5,9));
}
//练习一,模拟trim去两边空格
public static String myTrim(String str)//注意返回值String
{
int start=0, 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
}
//练习二,将字符串反转
public static String reverseString(String s, int start,int end) //反转一部分
{
char[] chs = s.toCharArray();// 1,将字符串变成数组
reverse(chs,start,end); // 2,对数组反转,细分功能
return new String(chs); // 3,将数组变成字符串
}
public static String reverseString(String s)//重载 //反转全部
{
return reverseString(s,0,s.length()-1); //重载
}
public static void reverse(char[] arr, int x, int y)//头尾反转
{
for (int start=x, end=y ; start<end ;start++,end-- )//注意参数列表
{
swap(arr, start, end); //元素交换 ,再细分功能
}
}
public static void swap(char[] arr, int x, int y)
{ //异或
char temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
【小结】
1,抽象设计思想的应用;将函数功能尽可能的细分,使其更有条理,便于修改;
2,善用逻辑运算符,减少判断的次数,如练习一;
3,善用重载,如练习二,return时在重载中调用,使功能更加完善又不增加太多代码;
4,调用方法时别忘了加括号();
三,获取一个字符串在另一个字符串中出现的次数。
"abkkcdkkefkkskk"思路:
1,定义个计数器。
2,获取kk第一次出现的位置。
3,从第一次出现位置后剩余的字符串中继续获取kk出现的位置。
每获取一次就计数一次。
4,当获取不到时,计数完成。
class StringTest2
{
public static void sop(String str)
{
System.out.println(str);
}
public static void main(String[] args)
{
String str = "ffadffassffadffsff";
//sop("count="+getSubCount(str,"ff"));
sop("count="+getSubCount2(str,"ff"));
sop("count="+str.split("ff").length);//不使用split的原因是当要截取的字符串刚好处于第一位时
//回多出来一个""空字符串,占用一个角标,引起计数不准确
}
//练习三 获取一个字符串在另一个字符串中出现的次数
public static int getSubCount(String str, String key)//子字符串从零开始判断
{
int count = 0;//计数器
int index = 0;//角标
while( (index= str.indexOf(key)) !=-1 )//注意参数:index是变化的可以在一个条件里面同时加入赋值和判断要加()
{
sop("str="+str);
str = str.substring(index+key.length());//从出现位置开始,往后继获取,
//每获取一次就出现一个新的子字符串,循环
count++;
}
return count;
}
//int indexOf( int ch); 返回ch在字符串中第一次出现的位置,如上所示
//int indexOf( int ch, int formIndex); 从fromIndex指定位置开始,获取ch字符在字符串中第一次出现的位置
//更高效的方法
public static int getSubCount2(String str ,String key)
{
int count = 0;
int index = 0;
while ((index= str.indexOf(key,index)) !=-1)
{
sop("index="+index);
index = index + key.length();
count++;
}
return count;
}
}
1,对数组进行操作时,操作角标比操作元素本身更为高效
2,获取次数是一个计数器思想的应用
3,使用split方法时要注意:无论截取位置位于字符串中哪一个位置,每截取一次都会增加一个子串,在开头和结尾就增加一个空字符串""。
四,获取两个字符串中最大相同子串。
第一个动作:将短的那个串进行长度一次递减的子串打印。
"abcwerthelloyuiodef"
"cvhellobnm"
思路:
1,将短的那个子串按照长度递减的方式获取到。
2,将每获取到的子串去长串中判断是否包含,如果包含,已经找到!。
class StringTest3
{
public static void sop (String str)
{
System.out.println(str);
}
public static void main(String[] args)
{
String s1= "dadfhelloasdd";
String s2= "fghelloggr";
sop(getMaxSubString(s1,s2));
}
public static String getMaxSubString(String s1,String s2)
{
String max = "",min = ""; //比较大小,处理较短的字符串,控制循环次数
max = (s1.length()>s2.length())?s1:s2;
min = (max==s1)?s2:s1;
for (int x =0 ; x<min.length() ;x++ ) //大圈套小圈思想
{
for (int y=0, z=min .length()-x; z!=min.length()+1 ; y++,z++ )//两个变量控制字符段右移,包含头不包含尾,所以尾角标+1
{
String temp = min.substring(y,z);
if (max.contains(temp))
return temp;
}
}
return "";//返回值明确为String类型,不能省略return
}
}
【小结】
1,灵活运用运算符,例如使用三元运算付替代if语句,使用逻辑运算符控制参数
2,循环的参数列表,可以有多个参数来控制循环
3,有明确返回值的函数一定不能省略return