<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # JAVA之旅(十六)——String類,String常用方法,獲取,判斷,轉換,替換,切割,子串,大小寫轉換,去除空格,比較 * * * > 過節耽誤了幾天,我們繼續JAVA之旅 ## 一.String概述 > String時我們很常用的數據類型,他也是一個類的使用 ![這里寫圖片描述](http://img.blog.csdn.net/20160611123924843) > 我們來看 ~~~ package com.lgl.hellojava; //公共的 類 類名 public class HelloJJAVA { public static void main(String[] args) { /** * String */ /** * s1是一個類類型變量,“abc”是一個對象 字符串最大的特點是,一旦被初始化,就不可以被改變 */ String s1 = "abc"; s1 = "kk"; System.out.println(s1); } } ~~~ > 為什么說初始化之后不可以改變,我們又重新賦值,輸出多少?肯定是kk,那不是變了嗎? * 這里注意,他是s1變了,但是這個abc這個對象還是abc > 這個要搞清楚,s1開始指向的abc后來指向kk而已 > > 我們再來對比一下 ~~~ package com.lgl.hellojava; //公共的 類 類名 public class HelloJJAVA { public static void main(String[] args) { String s1 = "abc"; String s2 = new String("abc"); System.out.println(s1 == s2); System.out.println(s1.equals(s2)); } } ~~~ > 輸出的結果? ![這里寫圖片描述](http://img.blog.csdn.net/20160611125443802) > 我們可以發現,==是不正確的,因為他是比較地址,而equals,則是比較值 > > 為什么? * String類復寫了object類中的equals方法,定義了自己獨特的內容,該方法用于判斷字符串是否相同 > 那s1和s2有什么區別? * s1代表一個對象 * s2代表兩個對象(new 和 abc) ## 二.String常用方法 > 我們知道了String的基本概述了,那我們就可以開始來學習他的一些常用的方法了,我們還是以例子為準 ~~~ package com.lgl.hellojava; //公共的 類 類名 public class HelloJJAVA { public static void main(String[] args) { String s1 = "abc"; String s2 = new String("abc"); String s3 = "abc"; System.out.println(s1 == s2); System.out.println(s1 == s3); } } ~~~ > 這里大家知道輸出的是什么嘛 ![這里寫圖片描述](http://img.blog.csdn.net/20160611130610728) > s1 = s3 為true是因為當內存中存在了對象就不會再創建了 > > String是用于描述字符串事物,那么它就提供了多個方法的對字符串進行操作 常見的操作有哪些?我們來分析一下 * 1.獲取? * 字符串中包含的字符數,也就是字符串的長度,也就是int length()獲取長度 * 根據位置獲取位置上的某個字符,也就是char charAt(int index) * 根據字符獲取該字符在字符串的位置 int indexOf(int ch),返回的是ch在字符串中第一次出現的位置 * int indexOf(int ch , int fromIndex):從fromIndex指定位置開始,獲取ch在字符串中出現的位置 * 根據字符串獲取該字符在字符串的位置 int indexOf(String str),返回的是ch在字符串中第一次出現的位置 * int indexOf(String str , int fromIndex):從fromIndex指定位置開始,獲取ch在字符串中出現的位置 * 2.判斷 * 字符串是否包含某一個子串 > boolean contains(str):判斷字符串是否存在? > 特殊之處:indexOf(str)可以索要str第一次出現的位置,返回-1的話,表示str不再字符串中存在,索要,也可以用于對指定判斷是否包含,if(str.indexOf(“aa”) != -1) * 字符串中是否有內容 > Boolean isEmpty():原理就是判斷長度是否為0 * 字符串是否是以指定的內容開頭 > boolean startWith(String str) * 字符串是否是以指定的內容結尾 > boolean startWith(String str) * 3.轉換 * 將字符數組轉換成字符串 > 構造函數String(char [])? > 構造函數(char [] , offset ,count)將字符數組中的一部分轉成字符串? > 靜態方法static String copyValueOf(char [] )? > 靜態方法static String copyValueOf(char [],int offset,int count ) * 將字符串轉換成字符數組 > char [] toCharArray() * 講字節數組轉成字符串 > 構造函數String(byte[])? > 構造函數(byte[] , offset ,count)將字節數組中的一部分轉成字符串 * 將字符串轉成字節數組 > byte [] getBytes() * 將基本數據類型轉換成字符串 > String valueOf(xxx); * 4.替換 * String replace(oldchar,newchar); * 5.切割 * String [] split(regex); * 6.子串 > 獲取字符串中的一部分? > String subString(begin)? > String subString(begin,end) * 7.大小寫轉換,去除空格,比較 * 將字符串轉換成大小寫 > String toUuperCase()? > String toLowerCase(); * 將字符串兩端的多個空格去掉 > String trim(); * 對兩個字符串進行自然順序的比較 > int compareTo(String) > 我們可以對獲取做一個小演示 ~~~ package com.lgl.hellojava; //公共的 類 類名 public class HelloJJAVA { public static void main(String[] args) { method_get(); } /** * String操作演示 */ public static void method_get() { String str = "abcdef"; //長度 sop(str.length()); //根據索引獲取字符 //當訪問到字符串中不存在角標的時候會發生錯誤:StringIndexOutOfBoundsException角標越界 sop(str.charAt(3)); //根據字符獲取索引 //沒有角標不會報錯,返回-1 sop(str.indexOf('d')); //反向索引一個字符出現的位置 sop(str.lastIndexOf('c')); } // 輸出語句 public static void sop(Object obj) { System.out.println(obj); } } ~~~ > 輸出的結果 ![這里寫圖片描述](http://img.blog.csdn.net/20160611230407101) > 我們再來看看判斷的小例子 ~~~ /** * 判斷 */ public static void method_is() { String str = "LiuGuiLin"; // 判斷是以Liu開頭 sop(str.startsWith("Liu")); // 判斷是以Lin結尾 sop(str.endsWith("Lin")); // 判斷是否存在Gui sop(str.contains("Gui")); } ~~~ > 我們的輸出 ![這里寫圖片描述](http://img.blog.csdn.net/20160611232034477) > 字符串和字節數組在轉換過程中是可以指定編碼表,我們可以看一下轉換的小例子 ~~~ /** * 轉換 */ private static void method_trans() { // 字符數組 char[] arr = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' }; // 轉換成字符串 String str = new String(arr); sop("str = :" + str); // 截取 String str1 = new String(arr, 1, 3); sop("str1 = :" + str1); String str3 = "ddvdvdv"; char[] arr3 = str3.toCharArray(); for (int i = 0; i < arr3.length; i++) { sop("arr3 = :" + arr3[i]); } } ~~~ > 我們再來看下替換的方法 ~~~ /** * 替換 */ public static void method_replace() { String s = "Hello JAVA"; // 替換 String s1 = s.replace('J', 'A'); //如果要替換的字符不存在,返回的還是原串 //當然,也可以替換字符串,這里就不演示了 sop(s1); } ~~~ > 輸出的結果 ![這里寫圖片描述](http://img.blog.csdn.net/20160612211317461) > 當然,也是可以替換字符串的,這里就不演示了 > > 我們再來看切割的小例子 ~~~ /** * 切割 */ public static void method_split() { String string = "zhangsan,lisi,wangwu"; // 切割 String[] arr = string.split(","); for (int i = 0; i < arr.length; i++) { sop("arr = :" + arr[i]); } } ~~~ > 這里我們按照逗號區分 ![這里寫圖片描述](http://img.blog.csdn.net/20160612214743380) > 我們再來看下子串 ~~~ /** * 子串 */ public static void method_sub() { String ss = "ferfefqwdqXXFV"; sop(ss.substring(2)); sop(ss.substring(2, 5)); } ~~~ > 這個直接截圖。很簡單 ![這里寫圖片描述](http://img.blog.csdn.net/20160612222811505) > 好了我們再來演示最后幾個方法的功能來結束本篇博客 ~~~ /** * 最后幾個 */ public static void method_7() { String st = " Hello Java And Android "; // 轉換大寫 sop(st.toUpperCase()); // 轉換小寫 sop(st.toLowerCase()); //去掉空格 sop(st.trim()); //比較 String st1 = "acc"; String st2 = "aaa"; //一個相同 sop(st1.compareTo(st2)); } ~~~ > OK,這個也沒什么可難的,輸出 ![這里寫圖片描述](http://img.blog.csdn.net/20160612224637200) > 好的,本篇博客就先到這里了 ## 我的群:555974449,歡迎加入 版權聲明:本文為博主原創文章,博客地址:http://blog.csdn.net/qq_26787115,未經博主允許不得轉載。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看