java中String的一些常見方法深入解析

這篇文章主要介紹“java中String的一些常見方法深入解析”,在日常操作中,相信很多人在java中String的一些常見方法深入解析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”java中String的一些常見方法深入解析”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

網(wǎng)站的建設(shè)成都創(chuàng)新互聯(lián)公司專注網(wǎng)站定制,經(jīng)驗豐富,不做模板,主營網(wǎng)站定制開發(fā).小程序定制開發(fā),H5頁面制作!給你煥然一新的設(shè)計體驗!已為成都墻體彩繪等企業(yè)提供專業(yè)服務(wù)。

package countio;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;public class MyUtil {    /*
     * 工具類中的方法都是靜態(tài)方式訪問的 因此將構(gòu)造器私有不允許創(chuàng)建對象(絕對好習(xí)慣)     */
    private MyUtil(){        throw new AssertionError();
    }    /*
     * @param filename 文件名
     * @param word  字符串
     * @return 字符串在文件中出現(xiàn)的次數(shù)     */
    public static int countWorInFile(String filename,String word){        int counter=0;
        FileReader fr = null;
        BufferedReader br = null;        try{
            fr = new FileReader(filename);
            br = new BufferedReader(fr);//字符緩存輸入流(從文件--輸入-->程序)
            String line = null;            while((line = br.readLine())!= null){                int index = -1;                //每一次讀取到的字符串一定大于等于所要找的串hello  indexOf返回某個指定的字符串值在字符串中首次出現(xiàn)的位置
                while((line.length() >= word.length() && (index =line.indexOf(word))>=0)){
                    counter++;
                    System.out.println(index);//輸出4   4 
                    line = line.substring(index+word.length());//從0開始                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{            if(br != null){                try {
                    br.close();
                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();
                }
            }            //fr.close();同上進行先判斷后關(guān)閉  防止 還沒建立就關(guān)閉掉資源了                }        return counter;
        
    }    public static void main(String[] args){        /*
         * File.separator表示系統(tǒng)相關(guān)的分隔符,Linux下為:/ Windows下為:\\
         * heiwhellodohehellod(有2個hello)         */
        //String filename = "\\d:\\hello.txt\\";
        String filename = File.separator+"d:"+File.separator+"hello.txt"+File.separator;        int counter = countWorInFile(filename,"hello");
        System.out.println("字符串hello出現(xiàn)的次數(shù)是:"+counter);
    }
}

java中String的一些方法深入解析

1、public String(char[] c,begin,length).
從字符數(shù)組c的下標(biāo)begin處開始,將長度為length的字符數(shù)組轉(zhuǎn)換為字符串。
begin與length可以省略,即將字符數(shù)組c轉(zhuǎn)換為字符串。另:字符數(shù)組可改為字節(jié)數(shù)組byte[] b.
char[] c=new char[]{'j','y','6','a','4','t','9'}; 
String s1=new String(c); 
String s=new String(c,2,3); 
System.out.println(s1);
System.out.println(s);


2、public char[] toCharArray().
字符串裝換成字符數(shù)組。


3、public char charAt(int 下標(biāo)).
返回字符串中指定位置的字符。
String s="jkdfsdf";
char t=s.charAt(3);

4、public byte[] getBytes().
將一個字符串轉(zhuǎn)換成字節(jié)數(shù)組,其默認輸出為ASCII值,可通過char強制類型轉(zhuǎn)換輸出字節(jié)。String s="sjdfsdf";
byte[] b=s.getBytes();

5、public String trim().
清除字符串左右兩端的空格。
String s="skkgnsdfsd   ";
System.out.println(s.trim());

6、public int indexOf(String s,int index).
從字符串中查找指定位置之后指定的字符所在的位置。若不指定位置,則從頭開始。
String s="dgdgdg";
int n=s.indexOf("t");//從頭開始查找
int n1=s.indexOf("d",3);//從位置3處開始查找

7、public String substring(int beginindex,int endindex ).
截取所指定的從開始位置到結(jié)束位置的字符串,不包含結(jié)束字符。結(jié)束位置可以省略。
String s="sdgsgghd";
String s1=s.substring(2,4);
String s2=s.substring(2);

8、public String[] split(String s).
通過指定的字符分割字符串。
String s="dfgdhdfgdrhrhgdt";
String ss[]=s.split("d");
for(int i=0;i<ss.length;i++)
System.out.println(ss[i]);

9、public String toUpperCase()./public String toLowerCase().字符大小寫轉(zhuǎn)換。
String s="dfgdhdfgdrhrhgdt";
String s1=s.toUpperCase();//字符全大寫
String s2=s.toLowerCase();//字符全小寫

10、public boolean startsWith(String s)./public boolean endsWith(String s).檢測字符串是否是以指定的字符開始/結(jié)尾。
String s="dfdhffghrtgfjn mjg";
boolean t1=s.startsWith("e");
boolean t2=s.endsWith("h");

11、判斷字符串是否相等,區(qū)分大小寫:equals()。不區(qū)分大小寫equalsIgnoreCase().
String s="dfgdghdf";
String s1="sfsgsdu";
s.equals(s1);

12、public String replaceAll(String s,String s1).將字符串中的s都替換成s1.
String s="dfgdghdf";
String s1=s.replaceAll("d","f");

package stringtest;public class TestString {    public static void main(String[] args) {        char[] c=new char[]{'j','y','6','a','4','t','9'}; 
        String s1=new String(c); 
        String s=new String(c,2,3); 
    /*    System.out.println(s1.charAt(3));//a
        System.out.println(s);//6a4*/    
        /*char[] c1 = s1.toCharArray();
        for(int i=0; i<c1.length; i++){
            System.out.print(c1[i]+",");//j,y,6,a,4,t,9,
        }*/
        /*String result="";
        byte[] bytes = s1.getBytes();*/
        
        /*String ss="dgdgdg";
        int n=ss.indexOf("t");//從頭開始查找
        int n3=ss.indexOf("d",3);//從位置3處開始查找
        int n1=ss.indexOf("d",1);//從位置1處開始查找
        System.out.println(n);//-1
        System.out.println(n1);//2
        System.out.println(n3);//4*/        
        String dd="sdgsgghd";
        System.out.println(dd.substring(2,4));//gs
        System.out.println(dd.substring(2));//gsgghd    }
}

到此,關(guān)于“java中String的一些常見方法深入解析”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

新聞標(biāo)題:java中String的一些常見方法深入解析
本文路徑:http://www.muchs.cn/article6/ijsjog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT網(wǎng)站策劃、商城網(wǎng)站、自適應(yīng)網(wǎng)站、定制網(wǎng)站、動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站