HOW2J——JAVA基础——String练习2

1、操纵字符串

/*
原谅我因为练习太小,写到了一个类里
 */
public class StringTest {

	public static void main(String[] args) {
		/*
		问题:给出一句英文句子,得到一个新的字符串,每个单词的首字母都转换为大写
		思路:把字符串转为字符数组,将第一个单词首字母大写,之后把每个空格后的字母大写
		 */
		String s="let there be light";
		print(s);
		
		char[] ch=s.toCharArray();
		ch[0]-=32;
		for(int i=1;i<s.length()-1;i++){
			if(ch[i]==' ')
				ch[i+1]-=32;
		}
		
		String s_new=String.copyValueOf(ch);
		print(s_new);

		
		/*
		英文绕口令
		peter piper picked a peck of pickled peppers
		统计这段绕口令有多少个以p开头的单词
		思路:用spilt将字符串分段统计
		 */
		String s1="peter piper picked a peck of pickled peppers";
		String[] s1_split=s1.split(" ");//将每个单词保存至一个字符串
		
		int count=0;
		for(int i=0;i<s1_split.length;i++){
			if(s1_split[i].charAt(0)=='p')
				count++;
		}
		
		print("以p开头的单词个数是:"+count);
		
		/*
		把 lengendary 改成间隔大写小写模式,即 LeNgEnDaRy
		思路:把字符串转为字符数组,偶数位大写
		*/
		String s2="lengendary";
		char[] ch1=s2.toCharArray();
		for(int i=0;i<ch1.length;i++){
			if(i%2==0){
				ch1[i]-=32;
			}
		}
		print(s2+"间隔大小写后得到"+String.valueOf(ch1));
		
		/*
		把最后一个two单词首字母大写
		思路:通过lastIndexOf找到最后一个two的位置;
		然后转换为字符数组,改变t为大写;
		再转换为字符串。
		 */
		String s3="Nature has given us that two ears, two eyes, and but one tongue,"+ 
		"to the end that we should hear and see more than we speak";
		int index=s3.lastIndexOf("two");
		char[] ch2=s3.toCharArray();
		ch2[index]-=32;
		print(String.copyValueOf(ch2));//字符串太长,打印时需要中间换行,未实现
	}

	//打印字符串
	public static void print(String s){
		System.out.println(s);
	}
}

2、比较字符串

/*
创建一个长度是100的字符串数组
使用长度是2的随机字符填充该字符串数组
统计这个字符串数组里重复的字符串有多少种
思路:对字符串进行排序O(n2),排序完从头至尾扫描一次。
 */
public class StringCompare {

	public static void main(String[] args) {
		//调用String练习1中StringTest类的getStringArray方法,
		//创建长度为100,每个字符串长度为2的随机字符串数组
		int arrLen=100;  
		int strLen=2;  
		String[] arr=StringTest.getStringArray(arrLen, strLen);
		printStringArray(arr);
		
		//将重复的字符串保存至一个字符数组中
		String[] repeatStr=getRepeatString(arr);
		System.out.println("总共有"+repeatStr.length+"种重复的字符串");
		System.out.println("分别是:");
		printStringArray(repeatStr);
	}
	
	//一个字符串数组中重复的字符串的种类,忽略大小写
	public static String[] getRepeatString(String[] arr){
		//对字符串进行选择排序,调用String练习1中StringTest类的stringArraySort方法
		StringTest.stringArraySort(arr);
		
		/*
		 遍历数组每个元素,将其与之后的每个字符串比较,
		 如果出现不相等,跳出循环,
		 如果与该元素不相等的元素出现在它的下一个之后,则有重复,将该元素保存,
		 从不相等的元素再次开始判断,即i=j。
		 */
		StringBuilder repeatStr=new StringBuilder();
		for(int i=0,j;i<arr.length;i=j){
			for(j=i+1;j<arr.length;j++){
				if(!arr[j].equalsIgnoreCase(arr[i]))
					break;
			}
			if(j!=i+1)
				repeatStr.append(arr[i]+" ");
		}
		
		return repeatStr.toString().split(" ");//返回保存重复元素的字符串数组
	}	

	//打印字符串数组
	public static void printStringArray(String[] arr){
		for(int i=0;i<arr.length-1;i++){
			System.out.print(arr[i]);
			if((i+1)%20!=0)//20个字符串为一行
				System.out.print(" ");
			else
				System.out.println();//换行
		}
		System.out.println(arr[arr.length-1]);//打印最后一个元素
	}
}

猜你喜欢

转载自blog.csdn.net/yaocong1993/article/details/79936970