<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆心得

    記我所見,記我所想

    BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
      34 Posts :: 0 Stories :: 16 Comments :: 0 Trackbacks

     訪問字符串


    1.字符串常量    "Hello World!"

     2.String表示字符串常量

      用String表示字符串:
      String( char chars[ ] );
      String( char chars[ ], int startIndex, int numChars );
      String( byte ascii[ ], int hiByte );
      String( byte ascii[ ], int hiByte, int startIndex, int numChars );
      String使用示例:
      String s=new String() ; 生成一個(gè)空串

      下面用不同方法生成字符串"abc":
      char chars1[]={'a','b','c'};
      char chars2[]={'a','b','c','d','e'};
      String s1=new String(chars1);
      String s2=new String(chars2,0,3);
      byte ascii1[]={97,98,99};
      byte ascii2[]={97,98,99,100,101};
      String s3=new String(ascii1,0);
      String s4=new String(ascii2,0,0,3);

     3.用StringBuffer表示字符串

      StringBuffer( ); /*分配16個(gè)字符的緩沖區(qū)*/
      StringBuffer( int len ); /*分配len個(gè)字符的緩沖區(qū)*/
      StringBuffer( String s ); /*除了按照s的大小分配空間外,再分配16個(gè)
                   字符的緩沖區(qū)*/




     1.類String中提供了length( )、charAt( )、indexOf( )、lastIndexOf( )、getChars( )、getBytes( )、toCharArray( )等方法。

      
    public int length() 此方法返回字符串的字符個(gè)數(shù)
       public char charAt(int index) 此方法返回字符串中index位置上的字符,其中index 值的 范圍是0~length-1
       public int indexOf(int ch)
        public lastIndexOf(in ch)
      
      返回字符ch在字符串中出現(xiàn)的第一個(gè)和最后一個(gè)的位置
       public int indexOf(String str)
        public int lastIndexOf(String str)
      返回子串str中第一個(gè)字符在字符串中出現(xiàn)的第一個(gè)和最后一個(gè)的位置
       public int indexOf(int ch,int fromIndex)
    public lastIndexOf(in ch ,int fromIndex)
      返回字符ch在字符串中位置fromIndex以后出現(xiàn)的第一個(gè)和最后一個(gè)的位置
       public int indexOf(String str,int fromIndex)
        public int lastIndexOf(String str,int fromIndex)
      返回子串str中的第一個(gè)字符在字符串中位置fromIndex后出現(xiàn)的第一個(gè)和最后一個(gè)的位置。
       public void getchars(int srcbegin,int end ,char buf[],int dstbegin)
       srcbegin 為要提取的第一個(gè)字符在源串中的位置, end為要提取的最后一個(gè)字符在源串中的位置,字符數(shù)組buf[]存放目的字符串,    dstbegin 為提取的字符串在目的串中的起始位置。
      public void getBytes(int srcBegin, int srcEnd,byte[] dst, int dstBegin)
      參數(shù)及用法同上,只是串中的字符均用8位表示。

     2.類StringBuffer提供了 length( )、charAt( )、getChars( )、capacity()等方法。

      方法capacity()用來得到字符串緩沖區(qū)的容量,它與方法length()所返回的值通常是不同的。



       substring( )
       toLowerCase( )
       toUpperCase( )

      ◇
    public String contat(String str);
      用來將當(dāng)前字符串對象與給定字符串str連接起來。
      ◇ public String replace(char oldChar,char newChar);
      用來把串中出現(xiàn)的所有特定字符替換成指定字符以生成新串。
      ◇ public String substring(int beginIndex);
      public String substring(int beginIndex,int endIndex);
      用來得到字符串中指定范圍內(nèi)的子串。
      ◇ public String toLowerCase();
      把串中所有的字符變成小寫。
      ◇ public String toUpperCase();
      把串中所有的字符變成大寫。

     2.StringBuffer類提供的方法:

      append( )
      insert( )
      setCharAt( )

      如果操作后的字符超出已分配的緩沖區(qū),則系統(tǒng)會自動為它分配額外的空間。
      ◇ public synchronized StringBuffer append(String str);
      用來在已有字符串末尾添加一個(gè)字符串str。
      ◇ public synchronized StringBuffer insert(int offset, String str);
      用來在字符串的索引offset位置處插入字符串str。
      ◇ public synchronized void setCharAt(int index,char ch);
      用來設(shè)置指定索引index位置的字符值。

      注意:String中對字符串的操作不是對源操作串對象本身進(jìn)行的,而是對新生成的一個(gè)源操作串對象的拷貝進(jìn)行的,其操作的結(jié)果不影響源串。

      相反,StringBuffer中對字符串的連接操作是對源串本身進(jìn)行的,操作之后源串的值發(fā)生了變化,變成連接后的串。


       equals( )和equalsIgnoreCase( )
      它們與運(yùn)算符'= ='實(shí)現(xiàn)的比較是不同的。運(yùn)算符'= ='比較兩個(gè)對象是否引用同一個(gè)實(shí)例,而equals( )和equalsIgnoreCase( )則比較  兩個(gè)字符串中對應(yīng)的每個(gè)字符值是否相同。

     2.字符串的轉(zhuǎn)化

      java.lang.Object中提供了方法toString( )把對象轉(zhuǎn)化為字符串。

     3.字符串"+"操作

      運(yùn)算符'+'可用來實(shí)現(xiàn)字符串的連接:
      String s = "He is "+age+" years old.";
      其他類型的數(shù)據(jù)與字符串進(jìn)行"+"運(yùn)算時(shí),將自動轉(zhuǎn)換成字符串。具體過程如下:
      String s=new StringBuffer("he is").append(age).append("years old").toString();

      注意:除了對運(yùn)算符"+"進(jìn)行了重載外,java不支持其它運(yùn)算符的重載。


    posted on 2007-04-20 16:35 源自有緣 閱讀(6554) 評論(2)  編輯  收藏

    Feedback

    # re: java中字符串處理 2009-10-27 21:26 shaoshiyong
    不錯(cuò)!  回復(fù)  更多評論
      

    # re: java中字符串處理 2012-09-16 17:29 logitech
    很受用!  回復(fù)  更多評論
      


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲国产成人精品无码区二本 | 黄在线观看www免费看| 亚洲伊人久久成综合人影院| 美女无遮挡免费视频网站| 成熟女人牲交片免费观看视频| 亚洲一卡二卡三卡| 国产卡二卡三卡四卡免费网址| 67194在线午夜亚洲| 成人黄18免费视频| 国产精品亚洲一区二区在线观看 | 精品久久久久成人码免费动漫 | 亚洲最大在线视频| 我们的2018在线观看免费高清| 亚洲av无码片在线观看| 日韩精品免费电影| 免费一区二区三区在线视频| 久久精品国产69国产精品亚洲| 免费国产黄网站在线观看| 亚洲视屏在线观看| 免费无码黄动漫在线观看| 免费视频成人国产精品网站| 亚洲AV永久无码精品一百度影院 | 夫妻免费无码V看片| 高潮毛片无遮挡高清免费| 亚洲伊人久久大香线蕉综合图片 | 毛片免费在线播放| 美女视频黄.免费网址| 久久久久久a亚洲欧洲AV| 一二三四在线播放免费观看中文版视频 | 久久不见久久见免费影院www日本| 国产精品亚洲аv无码播放| 最近最新高清免费中文字幕| 亚洲男人天堂2022| 亚洲精品成人片在线播放| 久久WWW免费人成人片| 免费人成再在线观看网站 | 亚洲gv猛男gv无码男同短文| 成人在线视频免费| 久久免费香蕉视频| 亚洲永久网址在线观看| 亚洲熟妇中文字幕五十中出|