当前位置 : 主页 > 编程语言 > c++ >

String,StringBuilder类常用方法

来源:互联网 收集:自由互联 发布时间:2021-07-03
String类常用方法 /* *常量池 *ConstantPool() *静态字符串在常量池中创建,并尽量使用同一个对象,重用静态字符串 *对于重复出现的字符串直接量,JVM首先在常量池中查找,若存在即返回该
String类常用方法
/*
	 *常量池
	 *ConstantPool()
	 *静态字符串在常量池中创建,并尽量使用同一个对象,重用静态字符串
	 *对于重复出现的字符串直接量,JVM首先在常量池中查找,若存在即返回该对象
	 */
	 public void testConstantPool(){
	 	System.out.println("*****ConstantPool*****");
	 	String str1 = "hello";
	 	String str2 = "hello";
	 	String str3 = "java";
	 	System.out.println(str1==str2);
	 	System.out.println(str1==str3);
	 }
	 
	 /*
	  *内存编码及长度
	  *String在内存中采用Unicode编码,每个字符占用两个字节
	  *任何字符都算一个字符长度占用两个字节
	 */
	 public void testLength(){
	 	System.out.println("*****Length*****");
	 	String str1 = "hello";
	 	System.out.println(str1.length());
	 	String str2 = "北京欢迎你";
	 	System.out.println(str2.length());
	 }
	 
	 /*
	 *indexOf实现检索
	 *int indexOf(String str)在字符串中检索另一个字符串
	 *int indexOf(String str,int fromIndex)从字符串fromIndex位置开始检索
	 *int lastIndexOf(String str,int from)str在字符串中出现多次时,
	 *将返回最后一个出现的位置
	 */
	 public void testIndexOf(){
	 	System.out.println("*****IndexOf*****");
	 	String str = "you can you up";
	 	int index = str.indexOf("can");
	 	System.out.println(index);
	 	index = str.lastIndexOf("you");
	 	System.out.println(index);
	 }
	 
	 /*
	  *substring截取子串
	  *String substring(int beginIndex,int endIndex)从beginIndex开始到endIndex结束的子字符串
	  *String substring(int beginIndex)从下标beginIndex开始到结尾的的子字符串
	  */
	  public void testSubstring(){
	  	System.out.println("*****substring*****");
	  	String str = "http://gitee.com";
	  	String subStr = str.substring(7,15);
	  	System.out.println(subStr);
	  	
	  	subStr = str.substring(7);
	  	System.out.println(subStr);
	  }
	  
	  /*
	   *trim()去掉字符串的前导和后继空字符
	  */
	  public void testTrim(){
	  	System.out.println("*****trim*****");
	  	String str = " hello, java ";
	  	String strTrim = str.trim();
	  	System.out.println(str+"的长度为"+str.length());
	  	System.out.println(strTrim+"的长度为"+strTrim.length());
	  }
	  
	  /*
	   *charAt()方法返回指定位置的的字符
	  */
	  public void testCharAt(){
	  	System.out.println("*****charAt*****");
	  	String str = "one world,one dream!";
	  	for(int i=0;i
 

  
   StringBuilder封装可变的字符串,常用以下方法改变字符序列。
  

  
/*
	*StringBuilder append(String str)追加字符串
	*/
	public void testAppend(){
		System.out.println("*****append*****");
		StringBuilder strBuilder = new StringBuilder("Programming Language:");
		strBuilder.append("Java")
				  .append(" ")
				  .append("SQL");
		System.out.println(strBuilder.toString());
	}
	
	/*
	*StringBuilder insert(int dstOffset,String s)插入字符串
	*/
	public void testInsert(){
		System.out.println("*****insert*****");
		StringBuilder strBuilder = new StringBuilder("I Java to program");
		strBuilder.insert(1," use");
		System.out.println(strBuilder);
	}
	
	/*
	*StringBuilder delete(int start,int end)删除字符串
	*/
	public void testDelete(){
		System.out.println("*****delete*****");
		StringBuilder strBuilder = new StringBuilder("Java language is perfect!");
		strBuilder.delete(5,5+9);
		System.out.println(strBuilder);
	}
网友评论