欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一篇文章看懂Java字符串操作

 更新時(shí)間:2021年11月21日 11:39:42   作者:/少司命  
String是Java中的類(lèi),它提供一些預(yù)定義的方法,這些方法使基于字符串的問(wèn)題解決方案更加容易,下面這篇文章主要給大家介紹了關(guān)于Java字符串操作的相關(guān)資料,需要的朋友可以參考下

?字符, 字節(jié)與字符串

??字符與字符串

字符串內(nèi)部包含一個(gè)字符數(shù)組,String 可以和 char[] 相互轉(zhuǎn)換.

NO 方法名稱(chēng) 類(lèi)型 描述
1 public String(char value[]) 構(gòu)造 將字符數(shù)組中的所有內(nèi)容變字符串
2 public String(char value[],int offset,int count) 構(gòu)造 將部分字符數(shù)組的內(nèi)容變?yōu)樽址?/td>
3 public char charAt(int index) 普通 取得指定索引位置的字符串,索引從0開(kāi)始
4 public char[] toChararray() 普通 將字符串變?yōu)樽址麛?shù)組返回

代碼示例: 獲取指定位置的字符

 public static void main(String[] args) {
        String str = "hello" ;
        System.out.println(str.charAt(0));// 下標(biāo)從 0 開(kāi)始
        System.out.println(str.charAt(1));
        System.out.println(str.charAt(2));
        System.out.println(str.charAt(3));
 
    }

代碼示例: 字符串與字符數(shù)組的轉(zhuǎn)換

 public static void main(String[] args) {
        String str = "helloworld" ;
        // 將字符串變?yōu)樽址麛?shù)組
        char[] data = str.toCharArray() ;
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i]+" ");
        }
 
    }

public static void main(String[] args) {
        String str = "helloworld" ;
        // 將字符串變?yōu)樽址麛?shù)組
        char[] data = str.toCharArray() ;
        // 字符數(shù)組轉(zhuǎn)為字符串
        System.out.println(new String(data)); // 全部轉(zhuǎn)換
        System.out.println(new String(data,5,5)); // 部分轉(zhuǎn)換
    }

?代碼示例: 給定字符串一個(gè)字符串, 判斷其是否全部由數(shù)字所組成

public static boolean isNumberChar(String s) {
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            //判斷某個(gè)字符是不是數(shù)字
            if(c < '0' || c > '9') {
                return false;
            }
 
        }
        return true;
    }
 public static void main(String[] args) {
        String str = "124567";
        System.out.println(isNumberChar(str));
    }

public static void main(String[] args) {
        String str = "1d4567";
        System.out.println(isNumberChar(str));
    }

??字節(jié)與字符串

字節(jié)常用于數(shù)據(jù)傳輸以及編碼轉(zhuǎn)換的處理之中,String 也能方便的和 byte[] 相互轉(zhuǎn)換

NO 方法名稱(chēng) 類(lèi)型 描述
1 public String(byte bytes[]) 構(gòu)造 將字節(jié)數(shù)組變?yōu)樽址?/td>
2 public String(byte bytes[],int offset,int length) 構(gòu)造 將部分字節(jié)數(shù)組中的內(nèi)容變?yōu)樽址?/td>
3 public bye[] getBytes() 普通 將字符串以字節(jié)數(shù)組的形式返回
4

public byte[] getBytes(String charsetNAme)throws

UnsupportedEncodingException

普通 編碼轉(zhuǎn)化處理

代碼示例: 實(shí)現(xiàn)字符串與字節(jié)數(shù)組的轉(zhuǎn)換處理

public static void main(String[] args) {
        String str = "helloworld" ;
        // String 轉(zhuǎn) byte[]
        byte[] data = str.getBytes() ;
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i]+" ");
        }
        System.out.println();
        // byte[] 轉(zhuǎn) String
        System.out.println(new String(data));
    }

 public static void main(String[] args)  {
        byte[] bytes = {97,98,99,100};
        String str = new String(bytes,1,3);
        System.out.println(str);
 
    }

??小結(jié)

byte[] 是把 String 按照一個(gè)字節(jié)一個(gè)字節(jié)的方式處理, 這種適合在網(wǎng)絡(luò)傳輸, 數(shù)據(jù)存儲(chǔ)這樣的場(chǎng)景下使用. 更適合 針對(duì)二進(jìn)制數(shù)據(jù)來(lái)操作.

char[] 是吧 String 按照一個(gè)字符一個(gè)字符的方式處理, 更適合針對(duì)文本數(shù)據(jù)來(lái)操作, 尤其是包含中文的時(shí)候.

?字符串常見(jiàn)操作

??字符串比較

No 方法名稱(chēng) 類(lèi)型 描述
1 public boolean equals(Object anObject) 普通 區(qū)分大小的比較
2 public boolean equalsIanorecase(String anotherString) 普通 不區(qū)分大小寫(xiě)的比較
3 public int compareTo(String anotherString) 普通 比較兩個(gè)字符串大小關(guān)系

代碼示例: 不區(qū)分大小寫(xiě)比較

 public static void main(String[] args) {
        String str1 = "hello" ;
        String str2 = "Hello" ;
        System.out.println(str1.equals(str2)); // false
        System.out.println(str1.equalsIgnoreCase(str2)); // true 
    }

在String類(lèi)中compareTo()方法是一個(gè)非常重要的方法,該方法返回一個(gè)整型,該數(shù)據(jù)會(huì)根據(jù)大小關(guān)系返回三類(lèi)內(nèi)容:?

1. 相等:返回0.

2. 小于:返回內(nèi)容小于0.

3. 大于:返回內(nèi)容大于0。


 public static void main(String[] args) {
        System.out.println("A".compareTo("a")); // -32
        System.out.println("a".compareTo("A")); // 32
        System.out.println("A".compareTo("A")); // 0
        System.out.println("AB".compareTo("AC")); // -1
        System.out.println("劉".compareTo("楊"));
    }

compareTo()是一個(gè)可以區(qū)分大小關(guān)系的方法,是String方法里是一個(gè)非常重要的方法。

字符串的比較大小規(guī)則, 總結(jié)成三個(gè)字 "字典序" 相當(dāng)于判定兩個(gè)字符串在一本詞典的前面還是后面. 先比較第一 個(gè)字符的大小(根據(jù) unicode 的值來(lái)判定), 如果不分勝負(fù), 就依次比較后面的內(nèi)容

??字符串查找

從一個(gè)完整的字符串之中可以判斷指定內(nèi)容是否存在,對(duì)于查找方法有如下定義:

NO 方法名稱(chēng) 類(lèi)型 描述
1 public boolean contains(CharSequence s) 普通 判斷一個(gè)子字符串是否存在
2 public int indexOf(String str) 普通 從頭開(kāi)始查找指定字符串的位置,查到了返回位置的開(kāi)始索引,如果查不到返回-1
3 public int indexOf(String str,int fromIndex) 普通 從指定位置查找子字符串位置
4 public int LastIndexOf(String str) 普通 從后向前查找子字符串位置
5 public int LastIndexOf(String str, int fromIdex) 普通 從指定位置由后向前查找
6 public boolean startWith (String prefix) 普通 判斷是否以指定字符串開(kāi)頭
7 public boolean startWith(String prefix, int toffset) 普通 從指定位置開(kāi)始判斷是否以指定字符串開(kāi)頭
8 public boolean endWith(String suffix) 普通 判斷是否以指定字符串結(jié)尾

代碼示例: 字符串查找,最好用最方便的就是contains()

public static void main(String[] args) {
        String str = "helloworld" ;
        System.out.println(str.contains("world")); 
        System.out.println(str.contains("forld"));
    }

?代碼示例: 使用indexOf()方法進(jìn)行位置查找

public static void main(String[] args) {
        String str = "helloworld" ;
        System.out.println(str.indexOf("world")); // 5,w開(kāi)始的索引
        System.out.println(str.indexOf("bit")); // -1,沒(méi)有查到
        if (str.indexOf("hello") != -1) {
            System.out.println("可以查到指定字符串!");
        }
    }

?代碼示例: 使用indexOf()的注意點(diǎn)

 public static void main(String[] args) {
        String str = "helloworld" ;
        System.out.println(str.indexOf("l")); // 2
        System.out.println(str.indexOf("l",5)); // 8
        System.out.println(str.lastIndexOf("l")); // 8
    }

?代碼示例: 判斷開(kāi)頭或結(jié)尾

public static void main(String[] args) {
        String str = "**@@helloworld!!" ;
        System.out.println(str.startsWith("**")); // true
        System.out.println(str.startsWith("@@",2)); // ture
        System.out.println(str.endsWith("!!")); // true
    }

???字符串替換

使用一個(gè)指定的新的字符串替換掉已有的字符串?dāng)?shù)據(jù),可用的方法如下

No 方法名稱(chēng) 類(lèi)型 描述
1 public String replaceAll(String regex,String replacement) 普通 替換所有指定的內(nèi)容
2 public String replaceFirst(String regex, String replacement) 普通 替換首個(gè)內(nèi)容

代碼示例: 字符串的替換處理

 public static void main(String[] args) {
        String str = "helloworld" ;
        System.out.println(str.replaceAll("l", "_"));
        System.out.println(str.replaceFirst("l", "_"));
    }

?注意事項(xiàng): 由于字符串是不可變對(duì)象 , 替換不修改當(dāng)前字符串, 而是產(chǎn)生一個(gè)新的字符串

???字符串拆分

可以將一個(gè)完整的字符串按照指定的分隔符劃分為若干個(gè)子字符串。

NO 方法名稱(chēng) 類(lèi)型 描述
1 public String[] split(String regex) 普通 將字符串全部拆分
2 public String[] split(String regex,int limit) 普通 將字符串部分拆分

代碼示例: 實(shí)現(xiàn)字符串的拆分處理

  public static void main(String[] args) {
        String str = "hello world hello yu" ;
        String[] result = str.split(" ") ; // 按照空格拆分
        for(String s: result) {
            System.out.println(s);
        }
    }

代碼示例: 字符串的部分拆分

public static void main(String[] args) {
        String str = "hello world hello yu" ;
        String[] result = str.split(" ",2) ;
        for(String s: result) {
            System.out.println(s);
        }
    }

代碼示例: 拆分IP地址

public static void main(String[] args) {
        String str = "192.168.1.1" ;
        String[] result = str.split("\\.") ;
        for(String s: result) {
            System.out.println(s);
        }
    }

?注意事項(xiàng):

1. 字符"|","*","+"都得加上轉(zhuǎn)義字符,前面加上"\".

2. 而如果是"",那么就得寫(xiě)成"\\".

3. 如果一個(gè)字符串中有多個(gè)分隔符,可以用"|"作為連字符

代碼示例: 多次拆分

public static void main(String[] args) {
        String str = "name=zhangsan&age=18" ;
        String[] result = str.split("&") ;
        for (int i = 0; i < result.length; i++) {
            String[] temp = result[i].split("=") ;
            System.out.println(temp[0]+" = "+temp[1]);
        }
    }

???字符串截取

從一個(gè)完整的字符串之中截取出部分內(nèi)容??捎梅椒ㄈ缦拢?/p>

NO 方法名稱(chēng) 類(lèi)型 描述
1 public String substring(int beginIndex) 普通 從指定索引截取到結(jié)尾
2 public String substring(int beginIndex, int endIndex) 普通 截取部分內(nèi)容

代碼示例: 觀察字符串截取

public static void main(String[] args) {
        String str = "helloworld" ;
        System.out.println(str.substring(5));
        System.out.println(str.substring(0, 5));
    }

?注意事項(xiàng):

1. 索引從0開(kāi)始

2. 注意前閉后開(kāi)區(qū)間的寫(xiě)法, substring(0, 5) 表示包含 0 號(hào)下標(biāo)的字符, 不包含 5 號(hào)下標(biāo)

??其他操作方法

NO 方法名稱(chēng) 類(lèi)型 描述
1 public String trim() 普通 去掉字符串的左右空格,保留中間空格
2 public String toUpperCase() 普通 字符串轉(zhuǎn)大寫(xiě)
3 public String toLowerCase() 普通 字符串轉(zhuǎn)小寫(xiě)
4 public native String intern() 普通 字符串入池操作
5 public String concat(String str) 普通 字符串連接,等同于+,不入池
6 public int length() 普通 取得字符串長(zhǎng)度
7 public boolean isEmpty 普通 判斷是否為空字符串,但不是null,而是長(zhǎng)度0

代碼示例: 觀察trim()方法的使用

public static void main(String[] args) {
        String str = " hello world " ;
        System.out.println("["+str+"]");
        System.out.println("["+str.trim()+"]");
    }

?代碼示例: 大小寫(xiě)轉(zhuǎn)換

public static void main(String[] args) {
        String str = " hello%$$%@#$%world 哈哈哈 " ;
        System.out.println(str.toUpperCase());
        System.out.println(str.toLowerCase());
    }

?代碼示例: 字符串length()

 public static void main(String[] args) {
        String str = " hello%$$%@#$%world 哈哈哈 " ;
        System.out.println(str.length());
    }

?注意:數(shù)組長(zhǎng)度使用數(shù)組名稱(chēng).length屬性,而String中使用的是length()方法

代碼示例: 觀察isEmpty()方法

public static void main(String[] args) {
        System.out.println("hello".isEmpty());
        System.out.println("".isEmpty());
        System.out.println(new String().isEmpty());
    }

?String類(lèi)并沒(méi)有提供首字母大寫(xiě)操作,需要自己實(shí)現(xiàn)

代碼示例: 首字母大寫(xiě)

 public static void main(String[] args) {
            System.out.println(fistUpper("yuisama"));
            System.out.println(fistUpper(""));
            System.out.println(fistUpper("a"));
        }
        public static String fistUpper(String str) {
            if ("".equals(str)||str==null) {
                return str ;
            }
            if (str.length()>1) {
                return str.substring(0, 1).toUpperCase()+str.substring(1) ;
            }
            return str.toUpperCase() ;
 
    }

總結(jié)

到此這篇關(guān)于Java字符串操作的文章就介紹到這了,更多相關(guān)Java字符串操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論