String类——练习

package cn.mmc.day13;

public class StringExcise {
/*
1,模拟一个trim方法,去除字符串两端的空格。
思路:
1,判断字符串第一个位置是否是空格,如果是继续向下判断,直到不是空格为止。
结尾处判断空格也是如此。
2,当开始和结尾都判断到不是空格时,就是要获取的字符串。


2,将一个字符串进行反转。将字符串中指定部分进行反转,"abcdefg";abfedcg
思路:
1,曾经学习过对数组的元素进行反转。
2,将字符串变成数组,对数组反转。
3,将反转后的数组变成字符串。
4,只要将或反转的部分的开始和结束位置作为参数传递即可。


*/


/*
3,获取一个字符串在另一个字符串中出现的次数。
"abkkcdkkefkkskk"


思路:
1,定义个计数器。
2,获取kk第一次出现的位置。
3,从第一次出现位置后剩余的字符串中继续获取kk出现的位置。
每获取一次就计数一次。
4,当获取不到时,计数完成。
*/


/*
4,获取两个字符串中最大相同子串。第一个动作:将短的那个串进行长度一次递减的子串打印。
"abcwerthelloyuiodef"
"cvhellobnm"
思路:
1,将短的那个子串按照长度递减的方式获取到。
2,将每获取到的子串去长串中判断是否包含,
如果包含,已经找到!。
*/


public static void sop(String str)
{
System.out.println(str);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = " ab cd ";
/*---------------------------------------------*/
sop("("+s+")");
// s = myTrim(s);
// sop("("+s+")");

sop("("+reverseString(s)+")");

/*---------------------------------------------*/
String str = "kkabkkcdkkefkks";
///sop("count====="+str.split("kk").length);不建议使用。
sop("count="+getSubCount_2(str,"kk"));

/*---------------------------------------------*/
String s1 = "ab";
String s2 = "cvhellobnm";
sop(getMaxSubString(s2,s1));
}

/*
练习四。
*/
public static String getMaxSubString(String s1,String s2)
{

String max = "",min = "";

max = (s1.length()>s2.length())?s1: s2;

min = (max==s1)?s2: s1;

// sop("max="+max+"...min="+min);
for(int x=0; x<min.length(); x++)
{
for(int y=0,z=min.length()-x; z!=min.length()+1; y++,z++)
{
String temp = min.substring(y,z);

sop(temp);
if(max.contains(temp))//if(s1.indexOf(temp)!=-1)
return temp;
}
}
return "";
}




/*
练习三。

*/

public static int getSubCount(String str,String key)
{
int count = 0;
int index = 0;

while((index=str.indexOf(key))!=-1)
{
sop("str="+str);
str = str.substring(index+key.length());

count++;
}
return count;
}

/*
练习三,方式二。

*/
public static int getSubCount_2(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,将数组变成字符串。
*/

public static String reverseString(String s,int start,int end)
{
//字符串变数组。
char[] chs = s.toCharArray();

//反转数组。
reverse(chs,start,end);

//将数组变成字符串。
return new String(chs);
}
public static String reverseString(String s)
{
return reverseString(s,0,s.length());

}

private static void reverse(char[] arr,int x,int y)
{
for(int start=x,end=y-1; start<end ; start++,end--)
{
swap(arr,start,end);
}
}
private static void swap(char[] arr,int x,int y)
{
char temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}

//练习一,去除字符串两端空格。
public static String myTrim(String str)
{
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);
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值