String--


package com.xdf.string;


import org.junit.Test;


public class StringDemo {


@Test
public void test01() {
String str1 = "hello";
String str2 = "HELLO";
System.out.println("變成大寫:" + str1.toUpperCase());
System.out.println("變成小寫:" + str2.toLowerCase());
System.out.println("字符串的長度:" + str1.length());
System.out.println("hello.equals(Hello)==>" + str1.equals(str2));
System.out.println("hello.equalsIgnoreCase(Hello)==>"
+ str1.equalsIgnoreCase(str2));
/*
 * 運行結果
 * 
變成大寫:HELLO
變成小寫:hello
字符串的長度:5
hello.equals(Hello)==>false
hello.equalsIgnoreCase(Hello)==>true
*/
}


/**
* 字符串轉換成  char類型的數組
*/
@Test
public void test02() {
String str1 = "h e l l o";
char[] strs = str1.toCharArray();
System.out.println(strs.length); // 數組的長度
for (char c : strs) {
System.out.print(c);
}
//運行結果:
//9
//h e l l o
}


/**
* 字符串拆分    String regex  正則表達式
*/
@Test
public void test04() {
String str1 = "h1-e2-l3-l4-o5";
String[] strs = str1.split("-");
for (String s : strs) {
System.out.println(s);
}
/*
* 運行結果:
* h1
e2
l3
l4
o5*/


}


/**
* 查詢指定字符的位置   下標從0開始
*/
@Test
public void test05() {
String str1 = "[email protected]";
System.out.println("@出現的位置:" + str1.indexOf("@"));
System.out.println(".最後一次出現的位置:" + str1.lastIndexOf("."));
//@出現的位置:6
//.最後一次出現的位置:13



}


/**
* 截取字符串  指向拿到 qq.com
*/
@Test
public void test06() {
String str1 = "[email protected]";
int begin = str1.indexOf("@");
int end = str1.lastIndexOf(".");
str1 = str1.substring(begin + 1, end); // 包含開始的位置,不包含結束的位置
System.out.println(str1);//結果:qq.com


}


/**
* 替換字符串   把 qq 換成  163
*/
@Test
public void test07() {
String str1 = "[email protected]";
str1 = str1.replace("qq", "163");
System.out.println(str1);//結果爲:[email protected]
}


/**
* 返回字符串中指定位置的字符
*/
@Test
public void test08() {
String str1 = "[email protected]";
System.out.println(str1.charAt(6));//結果爲:@
}


/**
*連接字符串  concat(String)
*/
@Test
public void test09() {
System.out.println("1" + 11 + 1);//1111
System.out.println(11 + 1 + "1" + 5.0 + 'a');//1215.0a
String str1 = "a";
String str2 = "bc";
System.out.println(str1.concat(str2));//abc
}


/**
* 判斷某個字符串中是否包含另一個完整的字符串
*/
@Test
public void test10() {
String str1 = "abcdefg";
System.out.println(str1.contains("cde"));//true 如果是afg 也是false的,要連接的字符
}


/**
* String  StringBuffer  StringBuilder
* String str1 = "abcdefg";
   str1 = "abc"; // 每次都是一個新對象


   01.String對象不可變
   02.StringBuffer  StringBuilder對象可變
   03.StringBuffer線程安全 但是 效率比StringBuilder低 ,適合於多線程的情況下使用
   04.StringBuilder 線程不安全,但是效率是最高的!   適合於單線程的情況下使用
*/
@Test
public void test11() {
String str1 = "hello"; // 定義一個變量
int num = 1000000; // 定義操作字符串的次數
// 設置開始時間
long begin = System.currentTimeMillis();
for (int i = 0; i < num / 100; i++) {
str1 += "bye";
}
// 設置結束時間
long end = System.currentTimeMillis();
System.out.println("String操作1W次執行的時間是====》" + (end - begin));


// 使用StringBuffer
str1 = "hello";
StringBuffer sb = new StringBuffer(str1);
// 設置開始時間
begin = System.currentTimeMillis();
for (int i = 0; i < num; i++) {
sb.append("bye");
}
// 設置結束時間
end = System.currentTimeMillis();
System.out.println("StringBuffer操作100W次執行的時間是====》" + (end - begin));
// 使用StringBuilder
str1 = "hello";
StringBuilder sb2 = new StringBuilder(str1);
// 設置開始時間
begin = System.currentTimeMillis();
for (int i = 0; i < num; i++) {
sb2.append("bye");
}
// 設置結束時間
end = System.currentTimeMillis();
System.out.println("StringBuilder操作100W次執行的時間是====》" + (end - begin));
}
@Test
public void test12(){


String s1="abc";
StringBuffer s2=new StringBuffer(s1);
System.out.println(s1.equals(s2));//打印爲false
/**
* 因爲主要的考察了String源代碼的equals方法有對參數  instanceof String 
* StringBuffer s2的祖先是CharSequence 所有不相等

*/
StringBuffer s3=new StringBuffer("abc");
System.out.println(s3.equals("abc"));//false
/**
* 因爲StringBuffer 沒有重寫equals方法   
* 所以是調用Object 的equals方法   當且僅當兩個對象地址相同 == 
* (也就是同一個對象) equals纔會返回true
*/
System.out.println(s3.toString().equals("abc"));//true
/**
* 因爲StringBuffer 重寫toString方法所有返回String
* 最後調用String equals的方法比較  所以返回true  可以看看String equals的源代碼

*/



}


}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章