天天看點

黑馬程式員------java基礎String

------Java教育訓練、Android教育訓練、iOS教育訓練、.Net教育訓練、期待與您交流! -------

String類

String s1 = "abc"//s1 是一個類類型變量,"abc是一個對象"。

字元串最大的特點:一旦被初始化就不可以被改變。

s1 = "kk";// s1指向變了,"abc"并未改變

__________________________________

String s1 = "abc";

Sttring s2 = new String("abc");

//s1和s2有什麼差別

s1在記憶體中有一個對象。

s2在記憶體中有2個對象。:new 了一個和"abc"

String s1 = "abc";

String s2 = new String("abc");

String s3 = "abc";

s1 == s3 :true;

"adc"在常量池中

//String 類複寫了Object類中的equals方法,

該方法用于判斷字元串是否相同。

String常見功能:

"abcd"

1 擷取和判斷

擷取:

   字元串中包含的字元數,也就是字元串的長度。

    int  length():擷取長度

   根據位置擷取位置上的某個字元。

    char charAt(int index)://當通路到字元串中不存在的腳标示會出現StringIndexOutOFBundensException

   根據字元擷取該字元在字元串中的位置

    int  indexof(int ch):傳回的是ch在字元串中第一次出現的位置。注釋:ch為字元的ASCALL碼

    int  indexof(int ch,int fromIndex):從fromIndex開始,傳回第一次出現ch的位置

    int  lastindexof(int ch);反向索引一個字元的位置。

    int  indexof(String str):傳回str在字元串中的位置

    int  indexof(String str,intformIndex);

    注釋:indexof(int ch); 當字元不在字元串中時傳回-1;

判斷:

    字元串中是否包含某個子串。    boolean  contains(str);

                                  特殊之處:indexof(str):可以索引str第一次出現的位置,如果傳回-1

                                  表示str不存在于字元串中,是以也可以用于對指定str判斷是否包含。

                                  if (str.indexof("aa")!=-1)

    字元串中是否有内容。          boolean  isEmpty();//當僅當length ==0時為真

    字元串是否以指定内容的開頭。  boolean  startsWith(String str);

    字元串是否以指定的内容結尾.   boolean  endsWith(String str);

    判斷字元串的内容是否相同,複寫了Object中的equals方法。

    boolean equals(str);

    判斷内容是否相同,并忽略大小寫。

    boolean equalsIgnoreCase(str);

轉換:

    1 将字元數組轉成字元串。

       構造函數:String(char[])

                 String (char[],offset,count):将字元數組中的一部分轉換成字元串。

       static String  copyValueof(char[]);

       static String  copyValueof (char[],int offset,int count);

    2 将字元串轉成字元數組。

                 char[] toCharArray();     

    3 将位元組數組轉換成字元串。?

         String(byte[])

         String(byte[],offset,count):将字元數組中的一部分轉換成字元串       

    4 将字元串轉成位元組數組。?

           byte[] getBytes();

    5 将基本資料類型轉換為字元串。

       static String valueof(int)

       static String valueof(double);

       //3 +"";//String.valueOf(3)

     特殊:字元串和位元組數組在轉換過程中是可以指定編碼的。

切割和替換:

      替換:

       String replace(oldchar,newchar);//如果要替換的字元不存在,傳回的還是原串

       String s = "hello java";

       String s1 = s.replace('a','n');//傳回的是一個新字元串

       此時  s 的值任然為  "hello java"

             s1的值為:"hello jnvn"     

       String  replace(char)

切割:

      String[] split(regex);//處理問題:String s = "zhangsan,lisi,wangwu";

      String arr[] = split s.split[","];      

子串:擷取字元串中的一部分

     String  substring(begin);//如果角标不存在,會出現字元串角标越界異常。

     String  substring(begin,end);//注:[begin,end);前閉後開

轉換,去除空格,比較

     1  将字元串轉換成大寫或者小寫

         String  toUpperCase();

         String  toLowerCase();

     2  将字元串兩端多個空格去除

         String trim();

     3  對2個字元串進行自然順序的比較

         int  compareTo(String);// 等于0,大于傳回正數,小于傳回負數

關于字元串的幾個練習

1  去除字元串2端多個空格

public  String  String myTrim(String str)
{
  int start =0;
  int end = str.length()-1;
   while(str.charAt(start)==' ')
       start ++;
   while(str.charAt(end)==' ')
      end--;
   
  return(str.subString(start,end+1));
}
           

2  将字元串反轉

public  String  String myTrim(String str)
{
   char[] arr = str.toCharArray();
   int start = 0;
   int end = arr.length -1;
   char temp = ' ';
   while(start<end)
   {
       temp = arr[start];
       arr[start] = arr[end];
       arr[end] = temp;

    }
   
  return new String(arr);
}
           

3  擷取一個字元串中另一個字元串出現的次數 

public int getSubStringCount(String str,String key)
{
   int count =0;
   int index =0;
   int length = key.length();
   while(str.indexOf(key,index)!=-1)
    {
     index += length;
     count ++;
    }
 return  count;
}
           

4  擷取2個字元串中最大相同子串

public  String getMaxSubString(String str1,String str2)
{ 

   String  max ="";
   String  min ="";
    max = str1.length()>str2.length()?str1:str2;
    min = max.length()>str1.lenght()?str1:str2;
    String temp = "";
   for(int x=0;x<min.length();x++)//必須保證str2是長度小的那個字元串以便減少循環次數
    {
      for(int a=0,b=min.length()-a;b!= min.length()+1;a++,b++)
       {
           temp = min.subString(a,b);
           if(max.indexOf(temp)!=1)
           {
                return temp;
           }
       }
    }
  return null;
}
           

StringBuffer:字元串緩沖器

     1 StringBuffer 是一個容器

       特點:

       1 長度可變

       2 可直接操作多個資料類型

       3 最終通過toString方法變成字元串;

       1 存儲

         StringBuffer append();指定資料作為參數添加到已有資料的結尾處

         StringBuffer insert(int offset,參數);

         :這裡的參數可以使出了byte  short類的任意資料類型

          可以将資料插入到offset位置

       2 删除

          StringBuffer delete(int start,int end):包含頭不包含尾

          StringBuffer deleteCharAt(index):删除指定位置的字元

                 delete(0,sb.length());//清空字元串緩沖區

       3 擷取

           char charAt(index);

           int  indexOf(String str);

           int  indexOf(String str,int fromIndex);

           int  lastIndexof(str);

           int  length();

           String  subString(start,end);

       4 修改

 *        StringBuffer replace(start,end,str);

 *        void setCharAt(index,char);替換某角标字元

 *        

 *     5 StringBuffer reverse();

StringBuilder :JDK1.5之後可使用

StringBuffer  是線程同步的

StringBuilder 是線程不同步的

單線程StringBuffer效率低

以後開發建議使用StringBuilder線程安全問題自己處理

java的更新:1 提高效率 2 簡化書寫  3 提高安全性