JavaString方法实例一
package string;
public class StringTest {
	public static void main(String[] args){
		String str = "sabdfasda4sbs";
		String str2 = "AasDfasdabs";
		StringBuffer str3 = new StringBuffer("sabdfasda4sbs");
		char[] data = {'h','e','l','l','o',' ','w','o','r','l','d'};
		//charAt(int index)    返回的是一个char
		//返回指定索引处的 char 值。
		System.out.println("str = "+str);
		System.out.println("str2 = "+str2);
		System.out.println("str3 = "+str3);
		System.out.println("char[] data = "+"'h','e','l','l','o',' ','w','o','r','l','d'" );
		System.out.println("str.charAt(3) = "+str.charAt(3));
		
		//codePointAy(int index)    返回的是一个int
		//返回指定索引处的字符(Unicode 代码点)。
		//Unicode 比ascll更全面。全球通用的。
		System.out.println("str.codePointAt(2) = "+str.codePointAt(2));
		//codePointBefor(int index) 返回的是一个int
		//返回指定索引之前一个字符(Unicode 代码点)。
		System.out.println("str.codePointBefore(2) = "+str.codePointBefore(2));
		System.out.println("str.codePointBefore(0)会报错,因为前面没有字符了");
		//compareTO(String anotherString) 返回的是一个int
		//按字典顺序比较两个字符串。
		//从第一个开始比较,要区分大小写,如果所有一样会返回0。
		//如果不一样就是从2个String的第一个位置开始比较,出现不同时,str的ASCll码减str2的ASCLL码
		//如果全部一样但是位数不一样,那么返回位数差
		System.out.println("str.compareTo(str2) = "+str.compareTo(str2));
		//compareToIgnoreCase(String str)   返回的是一个int
		//按字典比较两个字符串,不考虑大小写。
		//除了不考虑大小写其他和compareTo一样
		System.out.println("str.compareToIngoreCase(str2) = "+str.compareToIgnoreCase(str2));
		//concat(String str)   返回的是一个String
		//将指定字符串连接到此字符串的结尾。
		System.out.println("str.concat(\"concat\") = "+str.concat("concat"));
		//contains(CharSequence s)   返回一个boolean
		//当且仅当次字符串包含指定的char值序列号时,返回true
		//参数必须是字符串中有的,顺序必须一致,返回true。其他情况是false
		System.out.println("str.contains(\"a4s\") = "+str.contains("a4s"));
		System.out.println("str.contains(\"a44s\") = "+str.contains("a44s"));
		//contentEquals(CharSequence cs)   返回的是一个boolean
		//将此字符串与指定的 CharSequence 比较
		System.out.println("str.contentEquals(CharSequence cs)参数不知道怎么填");
		//contentEquals(StringBuffer sb)   返回的是一个boolean
		//将此字符串与指定的 StringBuffer 比较
		System.out.println("str.contentEquals(str3) = "+ str.contentEquals(str3));
		System.out.println("str2.contentEquals(str3) = "+ str2.contentEquals(str3));
		//copyValueOf(char[] data)   返回的是static String
		//返回指定数组中表示该字符序列的 String。	
		String newData = "";
		System.out.println("copyValueOf(data) = "+newData.copyValueOf(data));
		//copyValueOf(char[] data,int offset,int count)   返回的是static String
		//返回指定数组中表示该字符序列的 String。	相当于从第offset 位置添加 count 个
		String newData2 = "";
		System.out.println("copyValueOf(data,2,6) = "+newData.copyValueOf(data,2,6));
		//endsWith(String suffix)   返回boolean
		//测试此字符是否以指定的后缀结束。
		System.out.println(("str.endsWith(\"3sbs\") = ")+str.endsWith("3sbs"));
		System.out.println(("str.endsWith(\"4sbs\") = ")+str.endsWith("4sbs"));
		//equals(Object anObject)    返回boolean
		//将此字符串与指定的对象比较
		System.out.println("====================");
		String  a=new String("abc");
		String  b=new String("abc");
		String  c=new String("ABC");
		System.out.println("String  a=new String(\"abc\")");
		System.out.println("String  b=new String(\"abc\")");
		System.out.println("String  c=new String(\"ABC\")");
		System.out.println( "a == b  "+(a == b));
		System.out.println("a.equals(b)  "+a.equals(b));
		//equalsIgnoreCase(String anotherString)  返回boolean
		//将此 String 与另一个 String 比较,不考虑大小写。
		System.out.println("a.equalsIgnoreCase(b)  "+a.equalsIgnoreCase(b));
		System.out.println("a.equalsIgnoreCase(c)  "+a.equalsIgnoreCase(c));
		System.out.println("====================");
		//format(String format, Object... args)    返回  static String
		//使用指定的格式字符串和参数返回一个格式化字符串。
		String formatTest = null;
		System.out.println("String formatTest = null");
		System.out.println("formatTest.format(\"Hi,%s\", \"JAVA\") = "+formatTest.format("Hi,%s", "JAVA"));
		System.out.println("====================");
		//getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)   void
		//将字符从此字符串复制到目标字符(char[] dst)数组。
		String str5 = "13579024689090";
		char[] ch ={'a','b','c','d','e','f','g'};
		System.out.println("String str5 = \"13579024689090\"");
		System.out.println("char[] ch ={\'a\',\'b\',\'c\',\'d\',\'e\',\'f\',\'g\'}");
		str5.getChars(0, 5, ch,0);
		System.out.print("str5.getChars(0, 5, ch,0) = ");
		System.out.println(ch);
		str5.getChars(0, 5, ch,1);
		System.out.print("str5.getChars(0, 5, ch,1) = ");
		System.out.println(ch);
		//hashCode()   返回int
		//返回此字符串的哈希码。
		System.out.println("str5.hashCode() = "+str5.hashCode() );
		//indexOf(int ch)    返回int
		//返回指定字符在此字符串中第一次出现处的索引。
		System.out.println("str5.indexOf(\"90\") = "+str5.indexOf("90"));
		//indexOf(int ch, int fromIndex)   返回int
		//返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
		System.out.println("str5.indexOf(\"90,5\") = "+str5.indexOf("90",5));
		//intern()   返回String
		//  返回字符串对象的规范化表示形式。
		//一个字符串,内容与此字符串相同,但一定取自具有唯一字符串的池。
		System.out.println("str5.intern() = "+str5.intern());
		
	}
}
                    
                
                
            
        
浙公网安备 33010602011771号