String 相關操作

 * String類常用操作:

 * 拼接(+    concat

 * 查找 indexOf lastIndexOf  contains

 * 替換  replace

 * 比較字符串內容  equals,contentEquals

 * 判斷是否符合正則表達式:matches

 * 提取部分內容:substring

 * 格式化輸出 format

<h6>構建字符串對象:</h6>@Test
	public void test1() throws UnsupportedEncodingException{
		String s = "abc";//引用字符串緩衝池中的對象
		String s1 = new String("abc");
	
		byte[] content ="探險隊".getBytes("gbk");
		String s2 = new String(content,"utf-8");
		System.out.println(s2);//會亂碼
		
		String s3 = new String(s.getBytes());
		System.out.println(s3); //abc
		
		char[] c={'a','b','c'};
		String s4 = new String(c);
	}
<h6>倒序輸出</h6>@Test
	public void test2(){
		//獲取字符串長度
		//獲取指定位置字符
		//以倒序的形式將用戶輸入的字符串輸出
		String s = "我是一箇中國人";
		for (int i = s.length(); i >=0; i--) {
			System.out.println(s.charAt(i));
		}		
	}
<h6>判斷用戶輸入的是中文</h6>@Test
	public void test3(){//獲取unicode代碼點
		String s = "我是一箇中國人";
		//System.out.println(s.codePointAt(0));
		//	System.out.println(s.codePointAt(7));
		//判斷用戶輸入的都是中文 \u4e00---\u9fa5
		boolean c = true;
		for (int i = 0; i < s.length(); i++) {
			if(s.codePointAt(i)<'\u4e00'||s.codePointAt(i)>'\u9fa5'){
				c=false;
				break;
			}
		}
		System.out.println(c);
	}
<h6>字符串比較(字典順序)</h6>@Test
	public void test4(){
		String s ="abc";
		String s2 ="acd";
		System.out.println(s.compareTo(s2));			// -1
		
		s ="abc";
		s2 ="bcd";
		System.out.println(s.compareTo(s2));			//-1
		
		s ="abc";
		s2 ="Acd";
		System.out.println(s.compareTo(s2));			//32
		System.out.println(s.compareToIgnoreCase(s2));	//-1
		
	}
<h6>用concat進行字符串拼接</h6>@Test
	public void test5(){
		String a ="abc";
		String b = "def";
		String c = a.concat(b);//字符串拼接,產生一個新的字符串對象
		System.out.println(a);//字符串是不可變對象,不管對String對象做何操作,這個對象內容不會改變
		System.out.println(c);
		
	}
<h6>是否包含指定字符串/contentequals</h6>@Test
	public void test6(){
		String s = "jzm575444";
		System.out.println(s.contains("jzm"));//true
		String s2 = new String("jzm");
		System.out.println(s.contentEquals(s2));
	}
<h6>equals(Object o) return true/false on any type of data, depend if the content is equal ! 
contentEquals(CharacterSequence cs) returns true if and only if this String represents the same sequence of characters as the specifiedStringBuffer.</h6>
@Test
	public void testx(){
		String str1 = "Hello";
        String str2 = new String("Hello");
        StringBuilder str3 = new StringBuilder(str1);
 
        System.out.println(str1.equals(str2));//true
        System.out.println(str1.contentEquals(str2));//true
        System.out.println(str1.equals(str3));//false
        System.out.println(str1.contentEquals(str3));//true
	}
<h6>比較字符串相等</h6>@Test
	public void test7(){
		String uname = "張飛";
		String pwd = "123";
		Scanner input = new Scanner(System.in);
		String name = input.nextLine();
		String p = input.nextLine();
		if(uname.equals(name)&&pwd.equals(p)){
			System.out.println("登陸成功");
		}else{
			System.out.println("登陸失敗");
		}
	}
	
	@Test
	public void test8(){
		String s = "abc";
		String s2 = "abc";//字符串常量放在緩衝池,同一個字符串常量在池中只有一個
		String s3 = new String("abc");
		//==用來比較變量的值,引用對象的變量存儲的是引用的對象的地址
		//如果兩個引用類型變量的值相等,說明他們引用同一個對象
		System.out.println(s==s2);//true
		System.out.println(s==s3);//false
		System.out.println(s2==s3);//false
		//equals用來比較對象是否相等
		System.out.println(s2.equals(s3));//true
		
	}
	@Test
	public void test9(){
		String s = "a"+"b"+"c";//這個表達式中每個都是常量,字符串常量優化(編譯時優化)
		String s2 = "abc";
		System.out.println(s==s2);//true	
	}
	
	@Test
	public void test10(){
		String s = "a";
		String s2 = s +"b";//其中有變量,不會編譯時優化
		String s3 = "ab";
		System.out.println(s2==s3);//false	
	}
<h6>字符串格式化輸出</h6>
@Test
	public void test11(){
		//格式:張三的成績是89,體重是100.5
		/*String s = String.format("%s的成績是%d,體重是%.2f", "張三",89,100.5f);
		System.out.println(s);
		Calendar c = Calendar.getInstance();//日曆對象,可以進行日曆計算
		System.out.println(c.get(Calendar.YEAR));
		String cs = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);
		System.out.println(cs);*/
		String s =String.format("%2$6.3f,字符串是%s",5f,6f,"abc");//
		System.out.println(s);
		System.out.println(s.length());
	}	 
<h6>字符串出現處的索引</h6>	@Test
	public void test12(){
		String s = "aabbacc";
		System.out.println(s.indexOf("b"));//2   這個字符串第一次出現的索引
		System.out.println(s.indexOf("ba"));//3    這個字符串第一次出現的索引
		System.out.println(s.indexOf("bc"));//-1 未找到返回-1
		System.out.println(s.lastIndexOf('c'));//最後一次出現的索引
		System.out.println(s.indexOf('a',3)); //a第三次出現的索引
		
	}
<h6>字符串替換</h6>@Test
	public void test14(){
		String s = "aabbabac";
		//a替換爲d
		String s2 = s.replace('a', 'd');
		System.out.println(s);
		System.out.println(s2);
		String s3 = s.replace("ab","ww");
		System.out.println(s3);
		s = "aaa";
		s =s.replace("aa", "bb");//bba
		System.out.println(s);
	}
<h6>字符串拆分</h6>@Test
	public void test15(){
		String s = "a b c d";
		String[] c = s.split(" ");
		for (String st : c) {
			System.out.println(st);
		}
		s="張三89李四77王五98";
		c = s.split("\\d{1,}");
		for (String st : c) {
			System.out.println(st);
		}
	}
<h6>提取字符串/trim()</h6>@Test
	public void test16(){
		String s ="haha welcome you";
		//s.replace("welcome","");
		//提取5-8的字符
		System.out.println(s.substring(5,8));
		System.out.println(s.substring(5,9));
		//包括開始索引不包括結束索引
		System.out.println(s.substring(5));
		//trim()刪除字符串前後的空白
		s ="   abc   ";
		System.out.println(s.length());
		System.out.println(s.trim().length());
		//
		
	}
<h6> * String是一個不可變字符串對象,對String對象執行任何操作都會產生一個新的字符串對象
 *如果字符串的內容在程序運行時才能動態確定,使用StringBuffer或者Stringbuffer
 *StringBuffer 是線程安全的可變字符序列  效率低  可以使用在多線程環境
 *StringBuilder 是線程不安全的可變字符序列 效率高   只適合使用在單線程</h6> @Test
	public void test(){
		StringBuffer sb = new StringBuffer();
		sb.append("我是中國人");
		System.out.println(sb.toString());
		sb.append(true);
		System.out.println(sb.toString());
		sb.append(10);//末尾插入
		System.out.println(sb.toString());
		sb.insert(0, "haga");//指定位置插入
		System.out.println(sb.toString());
	}
@Test
	public void test2(){
		//要求接受用戶輸入的一個字符串,將它以倒序的方式輸出
		String s = "abcdefg";
		StringBuffer sb = new StringBuffer(s);
		s = sb.reverse().toString();
		System.out.println(s);
		String sql ="";
		sql+="select id,name from users";
		sql +="where sex=’男‘";
		sql += "name like '張'";
		//7個對象
		String s1 = "";
		for (int i = 0; i < Integer.MAX_VALUE; i++) {
			s1+="";
		}
	}


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