J2ME 基礎學習之字串

來源:互聯網
上載者:User

//J2ME  基礎學習之字串

//主要是String和StingBuffer//

//主要是字串的修改插入等操作

 import javax.microedition.lcdui.*;

 public class Strings extends Canvas
        {
        private String zifu= "我愛北京天安門";
        private String huizhi;
        private char[] zi={'你','是','我','的','女','人'};
        private byte[] fu={51,52,53,54,55,56};
        private StringBuffer nk=new StringBuffer("我愛你啊");
       
        private  char c;
        private byte[] bianhuan;
        public Strings ()
        {
        //定義方法 
        huizhi =new String(); // 空函數
        huizhi= new String(zifu); //以String value為身體
        huizhi= new String(zi);//以字元數組形成
        huizhi= new String(zi,2,2);//字元 其始  位移
        huizhi= new String(fu);//byte
        huizhi= new String(fu,3,3);
        huizhi= new String(nk);//StringBuffer
        huizhi= new String("我愛北京天安門");
        //讀取方法
        c=huizhi.charAt(3); //擷取對應位置字元
        bianhuan=new byte[6]; bianhuan=huizhi.getBytes();//轉化為位元組
        //void getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin) 
        huizhi.getChars(0,2,zi,2);
        //String substring(int beginIndex)
        //String substring(int beginIndex,ine endIndex)
        //huizhi=huizhi.substring(3,5);
        //比較boolean
        //int compartTo(String anotherString)
        //boolean startsWith(String prefix)
        //boolean endsWith(String prefix)
        //boolean equalsIgnoreCase(String anotherString)不分大小寫比較
        //boolean equals(Object anobject)
        //boolean regionMatches(boolean ignoreCase,int toffset,String other,int offset,int len)是否分大小寫比較
        //boolean regionMatches(int toffset,String other,int offset,int len)不分大小寫比較
       
        //字串搜尋方法
        //int indexOf(int ch,int fromIndex)
        //int indexOf(String str)
         //int indexOf(int ch)第一次搜尋到字元CH的位置
         //int indexOf(String str,int fromIndex)
        //int lastIndexOf(int char,int fromIndex)其他相同
       
        // 長度
        //int length()
        //String concat(String str) 效果與+相同
        //String replace(char oldchar,char newchar)
        //String toLowerCase()
        //String toUpperCase()//大小寫變換
        //String trim()去除首尾空格
       
       
        //StringBuffer 用法
        //StringBuffer()
        //StringBuffer(int length)長度為length
        //StringBuffer(String str)
       
        //添加append (boolean a)
        //append (char a) append (String a)
        nk.append(huizhi);
        huizhi= new String(nk);
        //插入字串
        //StringBuffer insert(int offset,boolean b)
        //StringBuffer insert(int offset,char b)
        //StringBuffer insert(int offset,String b)
       
        //StringBuffer delete(int start,int end)
         //StringBuffer deleteCharAt(int index)     
        //StringBuffer replace(int start,int end,String str)
       
        //String toString()將StringBuffer轉換為String
        //void setLength(int newLength)
        //int capacity()返回字串長度 但是緩衝長度
        //void ensureCapacity(int minimumCapacity)設定最小空間
        }
       
        protected void paint(Graphics g){
         g.setColor(0x00ffffff);
         g.fillRect(0,0,getWidth(),getHeight());
         g.setColor(0x00000000);
         g.drawString(String.valueOf(zi[3]),0,0,0);
         g.drawString(huizhi,0,30,0);
          g.drawChar(c,0,20,0);
         
                                       }
       }                                
          

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.