--String类


-

 

 

/**
 * String类适用于描述字符串事物;
 * 提供了多个方法对字符串进行操作:
 * 
 * 常见的操作:
 * 
 * 1.【获   取】
 * 		1.1:获取字符串的长度:
 * 			int length();
 * 		1.2:根据位置获取位置上的某个字符:
 * 			char charAt(int index);
 * 		1.3:根据字符获取该字符在字符串中的索引位置:(索引从0开始)
 * 			int indexOf(int ch);	//返回ch在字符串第一次出现的位置;此参数为字节码数字
 * 			int indexOf(int ch, int fromIndex)	//返回从fromIndex位置开始查找ch在字符串出现的位置;
 * 			int indexOf(String str) 	//返回str在字符串第一次出现的位置;
 * 			int indexOf(String str, int fromIndex) //返回从fromIndex开始查找str在字符串出现的位置;
 * 			
 * 			int lastIndexOf(int ch) 	//返回ch在字符串最后一次出现的位置
 * 				......

 * 2.【判   断】
 * 		2.1:判断字符串中是否包含某个子串;
 * 				boolean contains(str);
 * 				
 * 				特殊之处:indexOf(str)可以索引str第一次出现的位置,如果不包含str则返回 -1 ;
 * 						所以也可以用于判断是否包含:
 * 
 * 						if(string.indexOf(str) != -1)	//如果string包含str
 * 
 * 						而且该方法既可以获取索引,也可以判断是否包含;
 * 						
 * 		2.2:判断字符中是否有内容
 * 				boolean isEmpty() 
 * 		2.3:是否以指定的内容开头;
 * 				boolean startsWith(String prefix) 
 * 			//从指定索引开始的子字符串是否以指定前缀开始。
 * 				boolean startsWith(String prefix, int toffset)  
 * 		2.4:是否以指定的内容结尾;
 * 				 boolean endsWith(String suffix) 
 * 		2.5:判断字符串内容是否相同
 * 				boolean  equals(Object anObject)
 *  			boolean equalsIgnoreCase(String anotherString) //不考虑大小写
 *   
 *  3.【转   换】
 *  	3.1:将字符数组转化成字符串;
 *  		构造函数:
 *  				String(char[] value) 
 *  				//将字符数组中的一部分转换成字符串 offset:索引开始位置,count:个数
 *  				String(char[] value, int offset, int count) 
 *  
 *  		静态方法:
 *  				static String copyValueOf(char[] data) 
 *  				static String copyValueOf(char[] data, int offset, int count)  
 *  
 *  				static String valueOf(char[] data) 
 					static String valueOf(char[] data, int offset, int count) 
 
 *  	3.2:将字符串转换成字符数组;
 *  			 char[] toCharArray() 
 
 *  	3.3:将字节数组转换成字符串;
 *  		构造函数:
 *  				String(byte[] bytes)
 *  				String(byte[] bytes, int offset, int length) 

 *  	3.4:将字符串转换成字节数组;
 *  			byte[] getBytes() 
 				
 *  	3.5:将基本数据类型转换成字符串;
 *  		静态方法:
 *  				static String valueOf(boolean b) 
 *  				static String valueOf(int i) 
 *  				static String valueOf(double db) 
 *  				......
 * 		特殊:字符串和字节数组在转换过程中,是可以指定编码表的;
 * 
 * 4.【替   换】:
 * 			 String replace(char oldChar, char newChar) 
 *
 * 5.【切   割】:
 * 			 String[] split(String regex) 
 * 
 * 6.【子串获取】:获取字符串中的一部分:
 * 			 String substring(int beginIndex) 
 * 			 String substring(int beginIndex, int endIndex) 
 * 
 * 7.【转   换】【去除空格】【比   较】:
 * 		7.1:将字符串转成大写或小写:
 * 			 String toLowerCase() 	//转成小写
 * 			 String toUpperCase()   //转成大写
 * 
 * 		7.2:去除两端的空格:
 * 			 String trim() 
 * 
 * 		7.3:比较:对两个字符串进行自然顺序比较
 * 			int compareTo(String anotherString) 
 * 			
 */

package com.String;
public class StringDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		demo_7();
	}
	public static void demo_7()
	{
		String s = " Public Static ";
		String s1 = s.toLowerCase();	//全部小写
		String s2 = s.toUpperCase();	//全部大写
		String s3 = s.trim();			//去除两端的空格
		String s4 = s.replace(" ","");	//替换功能:去除所有的空格
		String s5 = "aaa" ;	
		String s6 = "aac" ;	
		
		sop(s1);						//	" public static "
		sop(s2);						//	" PUBLIC STATIC "
		sop(s3);						//	"Public Static"		
		sop(s4);						//	"PublicStatic"	
		sop(s5.compareTo(s6));
	}
	public static void demo_substring()	//获取字符串中的一部分
	{
		String s1 = "public static void main !";
		String s2 = s1.substring(7,13);	//包含头,不包含尾
		String s3 = s1.substring(7);	//从指定索引到结尾 如果指定索引不存在,则出现角标越界异常
		sop(s2);						//static
		sop(s3);						//static void main !
		
	}
	public static void demo_split()	// 切割
	{
		String s1 = "hallo java !";
		String[] str = s1.split(" ");	//切割 指定切割符为空格:" " 
		for(String s:str)
			sop(s);						
	}
	public static void demo_replace()	//替换
	{
		String s1 = "hello java!" ;
		String s2 = s1.replace("java", "world");//替换 
		sop(s2);		//hello world!
		sop(s1);		//hello java!
		
	}
	public static void demo_trans()	//转换
	{
		char[] chr = {'a','b','c','d','e'};
		String s = new String(chr,1,3);	//从索引1开始,把3个字符转换成字符串;
		sop("s = "+s);	//s = bcd
		sop("s = "+s.copyValueOf(chr, 2, 3));	//调用静态方法 copyValueOf ;
		sop("s = "+s.valueOf(chr));
	}
	public static void demo_is()	//判断
	{
		String s = "abcdefg.java" ;
		String s1 = "de" ;
		sop(s.contains(s1));	//是否包含
		sop(s.startsWith("abc"));	//是否以“abc”开头
		sop(s.endsWith(".java"));	//是否以“.java”结尾
	}
	public static void demo_1()
	{
		String s = "abc" ;		//s是一个‘类’类型的变量,“abc”是一个对象;
		//字符串最大的特点是:一旦被初始化就不能改变;
		
		String s1 = new String("abc");	//
		
		/*s和s1的区别:
		* s在内存中只有一个对象;
		* s1在内存在有2个对象;		
		*/
		System.out.println(s==s1);			//判断两个对象是否相同;
		System.out.println(s.equals(s1));	//String类复写了equals()方法,该方法用于判断字符串是否相同;
	}
	public static void demo_get()	//获取
	{
		String s = "abcdefg";
		sop(s.charAt(30));	//超出查找范围: java.lang.StringIndexOutOfBoundsException 角标越界异常
		sop(s.indexOf("y"));	//没有查到指定数据:返回 -1 ;
	}
	public static void demo_trim()	//去除空格
	{
		String s = " a  ";
		sop(s.length());
		String s1 = s.trim();	//去除前后的空格
		sop(s.trim());
		sop(s1.length());
	}
	public static void sop(Object obj)	//打印输出
	{
		System.out.println(obj);
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值