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

Java String函數(shù)的使用詳細(xì)介紹

 更新時(shí)間:2025年06月10日 14:41:00   作者:英雄不問(wèn)出處~  
今天我們來(lái)聊一下Java String函數(shù)的使用詳細(xì)介紹,本文通過(guò)通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

String

str本來(lái)是字符串常量的引用,應(yīng)該打印地址,但是編譯器重寫(xiě)了toString方法,所以打印hello

String 的構(gòu)造方法

public class test {
    public static void main(String[] args) {
        // 用常量字符串構(gòu)造
        String s1 = "hello";
        System.out.println(s1);
        // 用字符數(shù)組構(gòu)造
        char[] array = new char[]{'a','b','c'};
        String s2 = new String(array);
        System.out.println(s2);
        char[] array1 = new char[]{'a','b','c'};
        String s4 = new String(array,0,2);
        // 從0位置后拿2個(gè)字符
        System.out.println(s4);
        // 直接newSting對(duì)象構(gòu)造
        String s3 = new String("hello");
        System.out.println(s3);
    }
}

String對(duì)象在內(nèi)存中的情況

4. 空指針異常和空字符串,isEmpty()判斷是否為空字符串

字符串比

  • s1 == s2 比較地址
  • s1.equals(s2)比較是否相等,返回true或者false
  • s1.compareTo(s2)比較大小
  • s1.compareToIgonreCase(s2)忽略大小寫(xiě)比較
public class test {
    public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("Hello");
        System.out.println(s1 == s2);
        // 不等于,s1和s2表示對(duì)象的引用都存的是地址
        System.out.println(s1.equals(s2));
        System.out.println(s1.compareTo(s2));
        // s1 大于 s2 返回正數(shù)
        // s1 小于 s2 返回負(fù)數(shù)
        // s1 等于 s2 返回0
        System.out.println(s1.compareToIgnoreCase(s2));
        // 忽略大小寫(xiě)比較
    }
}

字符串查找

char charAt(int index),返回?cái)?shù)組中下標(biāo)對(duì)應(yīng)的字符

public static void main(String[] args) {
        String s1 = new String("hello");
        char ch = s1.charAt(1);
        System.out.println(ch);// e
    }

int indexOf(char ch),返回第一次出現(xiàn)ch字符的下標(biāo)

String s2 = new String("hello");
        int index = s1.indexOf('l');
        System.out.println(index);// 2

int indexOf(char ch,int k),k表示下標(biāo),從指定位置開(kāi)始查找

String s2 = new String("hello");
        int index = s1.indexOf('l',3);
        System.out.println(index);// 3

int indexOf(String s),可以查找子串在主串中出現(xiàn)的位置,如果沒(méi)有找到返回-1

String s3 = "ababcdeabcf";
        int index = s3.indexOf("abc");
        System.out.println(index);// 2
        int index1 = s3.indexOf("abc",3);
        System.out.println(index1);// 7 

int lastIndexOf(String s),倒著往前找,返回第一個(gè)找到的下標(biāo)

 String s3 = "ababcabcd";
        int index = s3.lastIndexOf("abc");
        System.out.println(index);// 5
        int index1 = s3.lastIndexOf("abc",4);
        System.out.println(index1);// 2
        // 從4下標(biāo)位置倒著往前找

轉(zhuǎn)化

數(shù)字和字符串之間的轉(zhuǎn)化

 String s2 = String.valueOf(new Student("zhangsan",20));
        System.out.println(s2);
        String s3 = String.valueOf(123);
        System.out.println(s3);
        String s4 = String.valueOf(123.34);
        System.out.println(s4);
        String s5 = String.valueOf(true);
        System.out.println(s5);// true

2. 字符串轉(zhuǎn)數(shù)字

 int a = Integer.parseInt("190");
        System.out.println(a);
 int b = Integer.parseInt("19.9");
 // 錯(cuò)誤,給的要是整數(shù)的字符串

大小寫(xiě)轉(zhuǎn)化

小寫(xiě)轉(zhuǎn)大寫(xiě):toUpperCase
轉(zhuǎn)變?yōu)榇髮?xiě)不是在原有字符串的基礎(chǔ)上轉(zhuǎn)換,而是轉(zhuǎn)變?yōu)榇髮?xiě)是一個(gè)新的對(duì)象,不會(huì)改變?cè)械淖址?/p>

String s = "hello";
 String ret = s.toUpperCase();
        System.out.println(ret);

大寫(xiě)轉(zhuǎn)小寫(xiě)

String s = "HEllo";
String ret = s.toLowerCase();
    System.out.println(ret);// hello

字符串轉(zhuǎn)為數(shù)組

String s = "hello";
  char[] ret = s.toCharArray();
  System.out.println(Arrays.toString(ret));
  // [h,e,l,l,o]

數(shù)組轉(zhuǎn)為字符串

char[] ret = {'a','b','c'};
    String s = new String(ret);
    System.out.println(s);// abc

格式轉(zhuǎn)換

String s = String.format("%d-%d-%d",2019,9,20);
System.out.println(s);// 2019-9-20

字符串替換

替換字符串

替換單個(gè)字符

3. 替換第一個(gè)ab

4. 把所有的ab都替換為123

字符串拆分

s.split()以這里面的字符串為標(biāo)準(zhǔn)分割

public static void main(String[] args) {
        String s = "hello world k";
        String[] array = s.split(" ");
        System.out.println(Arrays.toString(array));
        // [hello,world,k]
        String s1 = "hello world k";
        String[] array1 = s.split(" ",2);
        // 以空格分割最多分成兩組
        System.out.println(Arrays.toString(array1));
        // [hello, world k]
    }

特殊的情況,使用轉(zhuǎn)義字符

多次分割

字符串截?。ǔS茫?/h3>

substring(a,b) [a,b) a和b均為下標(biāo)

String str = "hello";
String ret = str.substring(0,3);// hel

給一個(gè)參數(shù),會(huì)把后面的全不截取

String str = "hello";
String ret = str.substring(0);// hello

trim(),去掉字符串的左右空格,中間的空格不可去掉

字符串的不可變性

  • 字符串中的value[] 數(shù)組是被private修飾的,也沒(méi)有提供get方法,在類(lèi)外是無(wú)法拿到的,就無(wú)法修改該數(shù)組了
  • 而被final修飾的,只是表明它是常量了,它的引用只能指向一個(gè)對(duì)象,不能被改變成指向別的對(duì)象
  • 被final修飾的不能被繼承
final int[] array = {1,2,3};
// array = new int[]{1,2};
// 不能改變array的指向了 
array[0] = 2;// 可被修改

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

相關(guān)文章

最新評(píng)論