zoukankan      html  css  js  c++  java
  • java.lang.String

    package com.etc.usual;
    
    import java.util.Arrays;
    
    public class TestString {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String str="qwesdzxc134532";
    		String str2="123456";
    		
    		//1、charAt(int index) 
    		char c = str.charAt(0);
    		//System.out.println("str.charAt(0): "+c); 
    		//System.out.println("str.charAt(13):"+ str.charAt(13)); 
    		for(int i=0;i<str.length();i++){//测试长度时,数组是不带()的length,字符串是带()的length
    			System.out.print(str.charAt(i)+",");//q,w,e,s,d,z,x,c,1,3,4,5,3,2,
    		}
    		
    		//2、compareTo(String anotherString) 
    		str="123456"; 
    		System.out.println("
    "+str.compareTo(str2));//0
    		
    		//3、concat(String str)
    		String str3=str.concat("ABCD");
    		System.out.println("str3:"+str3);//str3:123456ABCD
    		System.out.println("str:"+str);//str:123456 
    		
    		str+="QWER";
    		str2=str2.concat("QWER");
    		System.out.println("str+:"+str);//str+:123456QWER
    		System.out.println("str+:"+str2);//str+:123456QWER
    		
    		//4、str.contains(CharSequence cs)    //当且仅当此字符串包含指定的 char 值序列时,返回 true。 
    		System.out.println("contains:"+str.contains("123"));//contains:true
    		System.out.println("contains:"+str.contains("123QW"));//contains:false
    		
    		//5、endsWith(String suffix)     //测试此字符串是否以指定的后缀结束。
    		System.out.println("endsWith:"+str.endsWith("QWER"));//endsWith:true  
    		str="1234567890@sina.com";
    		System.out.println("endsWith:"+str.endsWith("@qq.com"));//endsWith:false
    		
    		//6、startsWith(String prefix)   //测试此字符串是否以指定的前缀开始。
    		System.out.println("startWith:"+str.startsWith("123"));//startWith:true 
    		System.out.println("startWith:"+str.startsWith("www."));//startWith:false 
    		
    		//7、equals(Object anObject)     //将此字符串与指定的对象比较。
    		str=new String("1234567890@sina.com");
    		str2=new String("1234567890@sina.com");
    		System.out.println(str+"----"+str2);//1234567890@sina.com----1234567890@sina.com
    		System.out.println("equals:"+str.equals(str2));//equals:true
    		System.out.println("==:"+(str==str2));//==:false  
    		
    		//8、indexOf(String str)     //返回指定字符在此字符串中第一次出现处的索引。
    		System.out.println("indexOf:"+str.indexOf("1"));//indexOf:0 
    		System.out.println("indexOf:"+str.indexOf('@'));//indexOf:10 
    		
    		//9、isEmpty()     // 当且仅当 length() 为 0 时返回 true。
    		System.out.println("isEmpty():"+str.isEmpty());//isEmpty():false
    		str="";
    		System.out.println("isEmpty():"+str.isEmpty());//isEmpty():true
    		str=" ";
    		System.out.println("isEmpty():"+str.isEmpty());//isEmpty():false
    		
    		//10、 lastIndexOf(String str)   //返回指定子字符串在此字符串中最右边出现处的索引。
    		str="12345678901111@sina123.com";
    		System.out.println("indexOf:"+str.indexOf("1")); //indexOf:0
    		System.out.println("lastIndexOf:"+str.lastIndexOf("1"));//lastIndexOf:19
    		
    		//11、length()   //返回此字符串的长度。 
    		System.out.println("length():"+str.length());//length():26
    		
    		//12、replace(char oldChar, char newChar)   //返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
    		System.out.println("str:"+str);//str:12345678901111@sina123.com
    		System.out.println("replace:"+str.replace("1","A"));//replace:A234567890AAAA@sinaA23.com
    		System.out.println("str:"+str);//str:12345678901111@sina123.com
    		
    		//13、split(String regex)  //根据给定正则表达式的匹配拆分此字符串。
    		str="123:45:678901111:@sina:123.com";
    		String[] sp = str.split(":"); 
    		System.out.println("split:"+Arrays.toString(sp));//split:[123, 45, 678901111, @sina, 123.com] 
    		
    		str="123,45,678901111,@sina,123.com";
    		System.out.println("split:"+Arrays.toString(str.split(",")));//split:[123, 45, 678901111, @sina, 123.com]
    		
    		//14、substring(int beginIndex)  //返回一个新的字符串,它是此字符串的一个子字符串。
    		//System.out.println("substring:"+str.substring(str.length()-5));  
    		
    		//15  toCharArray() //将此字符串转换为一个新的字符数组
    		char[] carr = str.toCharArray(); 
    		//System.out.println("toCharArray:"+Arrays.toString(carr));
    		
    		
    		//
    		String str11=new String("123"); 
    		System.out.println("str11:"+str11+"----"+str11.hashCode());//str11:123----48690
    		
    		str11=str11+"456";
    		System.out.println("str11:"+str11+"----"+str11.hashCode());//str11:123456----1450575459
    		
    		//
    		StringBuffer sb=new StringBuffer("123");
    		System.out.println("sb:"+sb.toString()+"----"+sb.hashCode());//sb:123----366712642 
    		
    		//sb.append("456"); 
    		StringBuffer sb2=new StringBuffer("123"); 
    		System.out.println("sb2:"+sb2+"----"+sb2.hashCode());//sb2:123----1829164700
    		
    		/**
    		 * String:是字符序列不可变的。
    		 * 
    		 * 相同点:都是字符序列可变。
    		 * StringBuilder是单线程安全
    		 * StringBuffer是多线程安全
    		 */
    	}
    }
    

      

  • 相关阅读:
    javascript异步编程系列【十】—Jscex+Easeljs制作坦克大战
    博客园分页JQuery打造的分页无刷新的Repeater
    参赛作品
    摄像机、投影、3D旋转、缩放
    javascript异步编程系列【八】Jscex版火拼俄罗斯
    javascript异步编程系列【七】扫盲,我们为什么要用Jscex
    javascript异步编程系列【五】Jscex制作愤怒的小鸟
    javascript异步编程系列【六】Jscex版愤怒的小鸟之冲锋陷阵鸟
    每周优秀代码赏析—Jscex内核【一】
    javascript异步编程系列【一】用Jscex画圆
  • 原文地址:https://www.cnblogs.com/1020182600HENG/p/6807351.html
Copyright © 2011-2022 走看看