String的构造方法和一般方法-成都快上网建站

String的构造方法和一般方法

public class StringTest{
    public static void main(String[] args){
        //1
        String s1 = "abc";
        //2
        String s2 = new String("cdg");
        //3
        byte[] bytes={98,88,34,25};
        String s3 = new String(bytes);
        //4String(byte[] bytes, int offset, int length)Constructs a new String by decoding the specified subarray of bytes using the platform's default charse;
        String s4 = new String(bytes,1,3);
        //5.
	char[] c1 = {'我','是','中','国','人'};
	String s5 = new String(c1);
	System.out.println(s5); //我是中国人
		
	//6.
	String s6 = new String(c1,2,2);
	System.out.println(s6); //中国System.out.println(s3); //abcd  String已经重写了Object中的toString
    }
}

/*字符串常用方法
*/

public class StringTest06{
	
	public static void main(String[] args){
		
		//1.char charAt(int index);
		String s1 = "我是王勇,是坏人!";
		char c1 = s1.charAt(2);
		System.out.println(c1); //王
		
		//2.boolean endsWith(String endStr);
		System.out.println("HelloWorld.java".endsWith("java")); //true
		System.out.println("HelloWorld.java".endsWith(".java")); //true
		System.out.println("HelloWorld.java".endsWith("HelloWorld.java")); //true
		
		System.out.println("HelloWorld.java".endsWith("txt")); //false
		System.out.println("HelloWorld.java".endsWith("java ")); //false
		
		//3. boolean equalsIgnoreCase(String anotherString);
		System.out.println("abc".equalsIgnoreCase("ABc")); //true
		
		//4.byte[] getBytes();
		byte[] bytes = "abc".getBytes();
		
		for(int i=0;i            
            
                                
本文标题:String的构造方法和一般方法
本文路径:http://kswjz.com/article/gesged.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流