Java——String练习(一)获取指定字符串中,另一个字符串出现的次数,并输出每次字符串出现位置的下标;首字母转成大写;获取指定字符串中,大写字母、小写字母、阿拉伯数字、其他字符的个数

题目:
1、获取指定字符串中,另一个字符串出现的次数,并输出每次字符串出现位置的下标;
2、将字指定符串的首字母转成大写,字符串中的其他字母转成小写;
3、获取指定字符串中,大写字母、小写字母、阿拉伯数字、其他字符的个数。

思路:
1、获取指定字符串中,另一个字符串出现的次数,并输出每次字符串出现位置的下标

思想:

  1. 用indexOf()找到到字符串中到第一次出现的索引
  2. 找到后,计数器++
  3. 用找到的索引+被找字符串长度,截取字符串

实现代码:

public static int getStringCount(String str, String key){
		//定义计数器
		int count = 0;
		//定义变量,保存indexOf查找后的索引的结果
		int index = 0;
		//开始循环找,条件,indexOf==-1 字符串没有了
		int indexnum = 0;
		while(( index = str.indexOf(key) )!= -1){
			count++;
			//获取到的索引,和字符串长度求和,截取字符串			
			str = str.substring(index+key.length());
			indexnum += (index+key.length());//获取截取的字符串中java字符串最后一个字母“a”所在位置的下标
			System.out.println("第"+count+"次查找“java”出现的下标是"+(indexnum - key.length()));
									//截取的字符串中包含java字符串,所以需要减去java字符串的长度5\12\19\24
		}
		return count;//返回出现次数
	}

2、将字指定符串的首字母转成大写,字符串中的其他字母转成小写

思想:

  • 获取首字母, charAt(0) 或者 substring(0,1)
    转成大写 toUpperCase()
  • 获取剩余字符串, substring(1)
    转成小写toLowerCase()

实现代码:

public static String toConvert(String str){
		//定义变量,保存首字母,和剩余字符
		String first = str.substring(0,1);//截取首个字符
		String after = str.substring(1);//截取剩余字符串
		//调用String类方法,大写,小写转换
		first = first.toUpperCase();
		after = after.toLowerCase();
		return first+after;//返回合并后的字符串
	}

3、获取指定字符串中,大写字母、小写字母、阿拉伯数字、其他字符的个数

思想:

  1. 计数器,就是int变量,满足一个条件 ++
  2. 遍历字符串, 用长度方法length() 和charAt() 遍历
  3. 字符判断是大写、是小写、是数字、还是其他字符

实现代码:

public static void getCount(String str){
		//定义四个变量,计数
		int upper = 0;
		int lower = 0;
		int digit = 0;
		int Other = 0;
		//对字符串遍历
		for(int i = 0 ; i < str.length() ; i++){
			//String方法charAt(),索引,获取字符
			char c = str.charAt(i);
			//利用编码表 65-90  97-122  48-57
			if(c >='A' && c <=90){
				upper++;
			}else if( c >= 97 && c <= 122){
				lower++;
			}else if(c>=48 && c<='9'){
				digit++;
			}else Other++;
		}
		System.out.print("大写字母个数:"+upper);
		System.out.print(",小写字母个数:"+lower);
		System.out.print(",阿拉伯数字个数:"+digit);
		System.out.println(",其他字符个数:"+Other);
	}
}

总代码:

public class StringExcise {
	public static void main(String[] args) {
		String s = "b24java,Zu.8*0javaFcjavaK";
		getCount(s);
		System.out.println(toConvert(s));
		System.out.println("java在字符串:"+ s +"中出现的次数是:"+getStringCount(s, "java"));
	}
	/*
	 *  获取一个字符串中,另一个字符串出现的次数,并输出每次字符串出现位置的下标;
	 *  1. 用indexOf()找到到字符串中到第一次出现的索引 	
		2. 找到后,计数器++
		3. 用找到的索引+被找字符串长度,截取字符串
	 */
	public static int getStringCount(String str, String key){
		//定义计数器
		int count = 0;
		//定义变量,保存indexOf查找后的索引的结果
		int index = 0;
		//开始循环找,条件,indexOf==-1 字符串没有了
		int indexnum = 0;
		while(( index = str.indexOf(key) )!= -1){
			count++;
			//获取到的索引,和字符串长度求和,截取字符串			
			str = str.substring(index+key.length());
			indexnum += (index+key.length());//获取截取的字符串中java字符串最后一个字母“a”所在位置的下标
			System.out.println("第"+count+"次查找“java”出现的下标是"+(indexnum - key.length()));
									//截取的字符串中包含java字符串,所以需要减去java字符串的长度5\12\19\24
		}
		return count;//返回出现次数
	}
	
	/*
	 *  将字符串的首字母转成大写,其他内容转成小写
	 *  思想:
	 *    获取首字母, charAt(0) 或者 substring(0,1)
	 *    转成大写 toUpperCase()
	 *    
	 *    获取剩余字符串, substring(1) 
		  转成小写toLowerCase()
	 */
	public static String toConvert(String str){
		//定义变量,保存首字母,和剩余字符
		String first = str.substring(0,1);//截取首个字符
		String after = str.substring(1);//截取剩余字符串
		//调用String类方法,大写,小写转换
		first = first.toUpperCase();
		after = after.toLowerCase();
		return first+after;//返回合并后的字符串
	}
	
	/*
	 * 获取指定字符串中,大写字母、小写字母、数字的个数。
	 * 
	 * 思想:
	 *   1. 计数器,就是int变量,满足一个条件 ++
	 *   2. 遍历字符串, 用长度方法length()和charAt() 遍历
	 *   3. 字符判断是大写、是小写、是数字、还是其他字符
	 */
	public static void getCount(String str){
		//定义三个变量,计数
		int upper = 0;
		int lower = 0;
		int digit = 0;
		int Other = 0;
		//对字符串遍历
		for(int i = 0 ; i < str.length() ; i++){
			//String方法charAt,索引,获取字符
			char c = str.charAt(i);
			//利用编码表 65-90  97-122  48-57
			if(c >='A' && c <=90){
				upper++;
			}else if( c >= 97 && c <= 122){
				lower++;
			}else if(c>=48 && c<='9'){
				digit++;
			}else Other++;
		}
		System.out.print("大写字母个数:"+upper);
		System.out.print(",小写字母个数:"+lower);
		System.out.print(",阿拉伯数字个数:"+digit);
		System.out.println(",其他字符个数:"+Other);
	}
}

输出结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值