练习一:写一个方法,实现去除字符串两端空格功能
package string;
/**
* 练习一:写一个方法,作用是去除两端空格,如同String.trim()
* @author asus
*
*/
public class StringDemo1 {
//示例方法,通过判断不断获取角标,最后进行剪切
public static String trimDemo(String s){
int start = 0;
int end = s.length()-1;
//检查字符串头部的空格,获取第一个非空格字符的角标
while(start<=end && s.charAt(start)==' '){
start++;
}
//检查字符串尾部的空格,获取第一个非空格字符的角标
while(start<=end && s.charAt(end)==' '){
end--;
}
//剪切获得去掉空格后的字符串,因为substring(start,end)方法剪切时不包含角标为“end”字符,所以这里要给“end+1”
return s.substring(start,end+1);
}
//自己写的方法,边判断边进行剪切,最终返回剪切好的字符串
public static String trimTest(String s){
String newStr = s;
//判断头部第一个字符是不是空格,若是则剪切
while((!newStr.equals("")) && newStr.charAt(0)==' '){
newStr = newStr.substring(1);
}
//判断尾部第一个字符是不是空格,若是则剪切
while((!newStr.equals("")) && newStr.charAt(newStr.length()-1)==' '){
newStr = newStr.substring(0,newStr.length()-1);
}
//返回剪切好的字符串
return newStr;
}
public static void main(String[] args) {
String s = " abc def ";
System.out.println("s = ["+s+"]");
//示例方法
String s1 = trimDemo(s);
System.out.println("s1 = ["+s1+"]");
//自己的方法
String s2 = trimTest(s);
System.out.println("s2 = ["+s2+"]");
}
}
package string;
/**
* 练习二:将字符串反转
* @author asus
*
*/
public class StringDemo2 {
//示例方法,反转整个字符串
public static String reverseString(String s){
return reverseString(s,0,s.length()-1);
}
//重载方法,反转字符串的指定部分
public static String reverseString(String s,int x,int y){
//将字符串转成字符数组
char[] ch = s.toCharArray();
//反转字符串
reverse(ch,x,y);
return new String(ch);
}
//根据角标指定范围反转字符数组
private static void reverse(char[] ch,int x,int y){
for(int start=x,end=y;start<end;start++,end--){
swap(ch,start,end);
}
}
//交换字符数组中两个字符的位置
private static void swap(char[] ch,int start,int end){
char temp = ch[start];
ch[start] = ch[end];
ch[end] = temp;
}
//自己写的方法
public static String myReverse(String s){
//将字符串转成字符数组
char[] ch = s.toCharArray();
//临时变量
char temp;
//反转字符数组
for(int i=0;i<=ch.length/2;i++){
temp = ch[i];
ch[i] = ch[ch.length-1-i];
ch[ch.length-1-i] = temp;
}
//将字符数组转成字符串
s = String.valueOf(ch);
return s;
}
public static void main(String[] args) {
String s = "abcdefgjjkkl";
System.out.println("原字符串: "+s);
String s1 = reverseString(s);
System.out.println("示例方法反转字符串: "+s1);
String s3 = reverseString(s,0,4);
System.out.println("示例方法反转字符串指定部分(前5个字母): "+s3);
String s2 = myReverse(s);
System.out.println("自己写的方法反转字符串: "+s2);
}
}
练习三:写一个方法,实现查找功能,查找一个字符串在另一个字符串中的出现次数
package string;
/**
* 练习三:查找一个字符串在另一个字符串中出现的个数
* @author asus
*
*/
public class StringDemo3 {
/*
* 示例方法1,
* 利用indexOf()方法查找是否包含字符串(返回值是-1表示没有找到),
* 然后再用substring()方法截取之前找过的部分继续查找,
* 最后计算查找到的次数
*/
public static int subCount(String str,String key){
int count = 0;
int index = 0;
while((index=str.indexOf(key))!=-1){
str = str.substring(index+key.length());
count++;
}
return count;
}
/*
* 示例方法2,
* 利用indexOf(String,int)方法在指定位置开始查找查找是否包含字符串(返回值是-1表示没有找到),
* 最后计算查找到的次数
*/
public static int subCount2(String str,String key){
int count = 0;
int index = 0;
while((index=str.indexOf(key,index))!=-1){
index = index+key.length();
count++;
}
return count;
}
/*
* 自己写的方法
* 利用split()方法会按着所给的字符串进行截取为字符串数组的特点,
* 如果没有则截取后数组长度为1,
* 否则找到的字符串个数就是数组长度-1,
* 注意:当结尾是要查到的字符串时,就需要不断的截取判断,并在原有值上+1
*
* 因为有时候split()会产生数组空项,因此不建议使用split()
*/
public static int myCount(String str,String s){
if(s==null||s.equals("")){
return 0;
}
String[] strArr = str.split(s);
if(strArr.length==1){
return 0;
}
int count = strArr.length-1;
while(str.endsWith(s)){
str = str.substring(0, str.length()-1);
count = count+1;
}
return count;
}
public static void main(String[] args) {
String str = "kkabkkcdkkefkkg";
String s = "kk";
System.out.println("源字符串:"+str);
System.out.println("要查找的字符串:"+s);
//示例方法1
int count2 = subCount(str,s);
System.out.println("示例方法1: "+count2);
//示例方法2
int count3 = subCount2(str,s);
System.out.println("示例方法2: "+count3);
//自己的方法
int count = myCount(str,s);
System.out.println("自己的方法: "+count);
}
}
练习四:写一个方法,实现查找功能,找出两个字符串中存在的最大子串
package string;
/**
* 练习四:获取两个字符串中,共同存在的最大的子串
* @author asus
*
*/
public class StringDemo4 {
//示例方法
public static String getMaxSubString(String s1,String s2){
//定义两个string变量
String max="",min="";
//将较大的赋值给max
max = (s1.length()>s2.length())?s1:s2;
//将另一个赋值给min
min = (max==s1)?s2:s1;
//外层for循环控制最多循环次数
for(int x=0;x<min.length();x++){
//从0角标位置开始,头和尾同时动,一直到尾部到达最大长度为止
for(int y=0,z=min.length()-x;z!=min.length()-1;y++,z++){
String temp = min.substring(y, z);
if(s1.contains(temp)){
return temp;
}
}
}
return null;
}
//自己写的方法
public static String myMaxSubstring(String str1,String str2){
//str2为小字符串
if(str1.length()<str2.length()){
String temp = str1;
str1 = str2;
str2 = temp;
}
//获取小字符串的长度
int str2_length = str2.length();
//外层for循环控制最多循环次数
for(int i=0;i<=str2.length();i++){
//内层for循环控制小字符串不断缩减长度后,在大字符串中的查找次数
for(int j=0;j<=i;j++){
//将小字符串截取后再查找
// System.out.println(str2.substring(j, str2_length+j));
if(str1.contains(str2.substring(j, str2_length+j))){
return str2.substring(j, str2_length+j);
}
}
//不断缩减长度查找
str2_length--;
}
return null;
}
public static void main(String[] args) {
String str2 = "bcdefghj";
String str1 = "aqwrtyj";
//示例方法
String sub2 = getMaxSubString(str1, str2);
System.out.println(sub2);
//自己的方法
String sub = myMaxSubstring(str1,str2);
System.out.println(sub);
}
}