String类

String 类:String 是字符串类型,它的数据存储方式和基本数据类型一样,String 又是一个类,String 类是final的,是不可继承的,String 类的本质是字符数组char[] ,其值是不可改变的,javaAPI中提供很多String类的方法;

 

Sting类方法的演示:

 

/**
 * String类的方法演示
 *
 */
public class StringDemo {
	
	public static void main(String[] args){
		
		String str = "abcabcab;cabcabc";
		//字符串的长度
		int  m = str.length();
		System.out.println("字符串的长度是: "+m);
		//输出字符串b第一次出现的位置
		System.out.println("The index of the character is: "+str.indexOf("b"));
		//输出指定字符串最后一次出现的位置
		System.out.println("The lastIndex of the character is: "+str.lastIndexOf("b"));
		//输出字符串从指定位置开始访问,第一次出现的位置
		System.out.println("The index of the character begin from index 2 is: "+str.indexOf("b",2)); 
		// 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
	    System.out.println("The lastIndex of the character reverse from index 12 is: "+str.lastIndexOf("b",12)); 
		//输出指定字符的位置的字符
		System.out.println("The character at index 2 is : "+str.charAt(2));
		//将指定字符串连接到此字符串的结尾处
		System.out.println("The new String after concat is: "+str.concat("hgj"));
		//按字典顺序比较两个字符串
		System.out.println("The result of the compare is: "+str.compareTo("abcdefg"));
		//是否包含指定的char型的可读序列
		System.out.println("The result of the contain return : "+str.contains("abcz"));
		//将此字符串与指定的stringBuffer比较
		System.out.println("The result of the contentEquals return: "+str.contentEquals("abcd"));
		//System.out.println(str.copyValueOf());
		//测试此字符串是否以指定的后缀结尾
		System.out.println("This String is ending with the string return: "+str.endsWith("abc"));
		//返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引。
		System.out.println("The index after  codePointOffset is : "+str.offsetByCodePoints(1, 4));//输出整数5
		//  返回指定索引处的字符(Unicode 代码点)。输出指定字符的AS码值
		System.out.println("The AS  value of  the index 2 is: "+str.codePointAt(2));
		//对字符串进行拆分
		String[] c =str.split(";");
		for(int i=0;i<c.length;i++ ){
		System.out.println("The new String after split are:"+c[i]);
		}
		//测试此字符串是否以指定的前缀开始。
		System.out.println("Whelther starts with the String return:  "+str.startsWith("a"));
		//返回一个新的字符串,它是此字符串的一个子字符串。
		System.out.println("The new String of the subSequence is: "+str.subSequence(2,4));
		//返回一个新的字符串,它是此字符串的一个子字符串。
		System.out.println("The new String of the substring is"+str.substring(2));
		//把字符串换成大写
		System.out.println("The new String after to upperCase is: "+str.toUpperCase());
		//把字符串换成小写
		System.out.println("The new String after to lowerCase is: "+str.toLowerCase());
		//返回字符串的副本,忽略前导空白和尾部空白。
		System.out.println("The new String after triming is : "+str.trim());
	  
	}

}

 输出结果:

 

字符串的长度是: 16
The index of the character is: 1
The lastIndex of the character is: 14
The index of the character begin from index 2 is: 4
The lastIndex of the character reverse from index 12 is: 11
The character at index 2 is : c
The new String after concat is: abcabcab;cabcabchgj
The result of the compare is: -3
The result of the contain return : false
The result of the contentEquals return: false
This String is ending with the string return: true
The index after  codePointOffset is : 5
The AS  value of  the index 2 is: 99
The new String after split are:abcabcab
The new String after split are:cabcabc
Whelther starts with the String return:  true
The new String of the subSequence is: ca
The new String of the substring iscabcab;cabcabc
The new String after to upperCase is: ABCABCAB;CABCABC
The new String after to lowerCase is: abcabcab;cabcabc
The new String after triming is : abcabcab;cabcabc

 

 

实例:

用String类中的方法输出一字符串中每个字符出现的次数。

解题思路:利用String类的方法,遍历字符串中的每个字符,每个字符都有对应的AS码值,定义一个长度为256的数组,数组的下标对应着AS码值,当遍历到一个字符,就在数组下标与该字符对应的AS码值相等的数组位置加1,当再次遍历到该字符就再加1,这样就把每个字符的出现次数都存在数组当中,最后输出至少出现1次的字符即可。

代码:

/**
 *定义一个StringShow2类
 */
public class StringShow2 {
    /**
     * 程序的入口
     * @param args
     */
	public static void main(String[] args){
		//定义一个String类型的字符串
		String str ="asdhhsfqygdshchhjjagbddhvcddvhjdvs";
		//求字符串的长度
		int m = str.length();
		System.out.println("字符串的长度是:"+m);
		//定义一个长度为256的数组a
		int [] a =new int[256];
		/**
		 * 循环控制次数
		 */
		for(int i=0;i<m;i++){
			//将输出的字符为int类型
			int chr= str.charAt(i);
			//输出的字符对应的AS码值存入数组中
			a[chr]++;
			//第几次输出字符
		System.out.println("第"+a[chr]+"次输出字符"+(char)chr);
		}
		System.out.println();
		//循环控制出现次数
		for(int j=0;j<a.length;j++){
			//只输出出现的字符
			if(a[j]!=0)
		  //输出各个字符出现的次数
			System.out.println((char)j+"输出的次数是:"+a[j]);
			
		}
	}

}

 输出结果:

 

字符串的长度是:34
第1次输出字符a        第1次输出字符s        第1次输出字符d           第1次输出字符h      第2次输出字符h       

第2次输出字符s        第1次输出字符f         第1次输出字符q           第1次输出字符y      第1次输出字符g      

第2次输出字符d        第3次输出字符s        第3次输出字符h           第1次输出字符c      第4次输出字符h     

第5次输出字符h        第1次输出字符j         第2次输出字符j            第2次输出字符a      第2次输出字符g    

第1次输出字符b        第3次输出字符d        第4次输出字符d           第6次输出字符h      第1次输出字符v      

第2次输出字符c        第5次输出字符d        第6次输出字符d          第2次输出字符v       第7次输出字符h    

第3次输出字符j         第7次输出字符d        第3次输出字符v          第4次输出字符s

 

a输出的次数是:2
b输出的次数是:1
c输出的次数是:2
d输出的次数是:7
f输出的次数是:1
g输出的次数是:2
h输出的次数是:7
j输出的次数是:3
q输出的次数是:1
s输出的次数是:4
v输出的次数是:3
y输出的次数是:1

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值