JAVA-String类

String类包括用于检查序列的各个字符的方法,用于比较字符串,搜索字符串,提取子字符串以及创建将所有字符翻译为大写或小写的字符串的副本。


内存案例

String 创建的字符串存储在公共池中,而 new 创建的字符串对象在堆上

    public static void main(String[] args) {
        String a1 = "XXXXX";              // String 直接创建
        String a2 = "XXXXX";              // String 直接创建
        String a3 = s1;                    // 相同引用
        System.out.println(a1==a2);//true

        String a4 = new String("XXXXX");   // String 对象创建
        String a5 = new String("XXXXX");   // String 对象创建
        System.out.println(a4==a5);//false

    }

String 类是不可改变的

String 是不可改变的,一旦创建了 String 对象,那它的值就无法改变了

String被final关键字修饰,意味着String类不能被继承, 并且它的成员方法都默认为final方法;字符串一旦创建就不能再修改

 


连接字符串(concat和+)

    public static void main(String[] args){
        String a1 = "卢大宝".concat("XXX");
        String a2 = "卢大宝" + "XXX";

        System.out.println(a1);//卢大宝XXX
        System.out.println(a2);//卢大宝XXX
    }

+ 可以连接 nullnull视为一个字符串连接,concat连接中出现null,会造成空指针错误

+号编译后就是使用了StringBuiler来拼接,所以使用+的语句就会创建一个StringBuilder,多条+语句在不同循环层中会创建多个StringBuilder对象,建议手动使用StringBuilder


格式化字符串

转  换  符说    明 示    例
%s字符串类型"mingrisoft"
%c字符类型'm'
%b布尔类型TRUE
%d整数类型(十进制)99
%x整数类型(十六进制)FF
%o整数类型(八进制)77
%f浮点类型99.99
%a十六进制浮点类型FF.35AE
%e指数类型9.38E+05
%g通用浮点类型(f和e类型中较短的)
%h散列码
%%百分比类型
%n换行符
%tx日期与时间类型(x代表不同的日期与时间转换符
    public static void main(String[] args){
        String str=null;
        str=String.format("Hi,%s", "王力");
        System.out.println(str);		//Hi,王力
        str=String.format("Hi,%s:%s.%s", "王南","王力","王张");
        System.out.println(str);		//Hi,王南:王力.王张
        System.out.printf("字母a的大写是:%c %n", 'A');		//字母a的大写是:A
        System.out.printf("3>7的结果是:%b %n", 3>7);		//3>7的结果是:false
        System.out.printf("100的一半是:%d %n", 100/2);		//100的一半是:50
        System.out.printf("100的16进制数是:%x %n", 100);		//100的16进制数是:64
        System.out.printf("100的8进制数是:%o %n", 100);		//100的8进制数是:144
        System.out.printf("50元的书打8.5折扣是:%f 元%n", 50*0.85);		//50元的书打8.5折扣是:42.500000 元
        System.out.printf("上面价格的16进制数是:%a %n", 50*0.85);		//上面价格的16进制数是:0x1.54p5
        System.out.printf("上面价格的指数表示:%e %n", 50*0.85);		//上面价格的指数表示:4.250000e+01
        System.out.printf("上面价格的指数和浮点数结果的长度较短的是:%g %n", 50*0.85);		//上面价格的指数和浮点数结果的长度较短的是:42.5000
        System.out.printf("上面的折扣是%d%% %n", 85);		//上面的折扣是85%
        System.out.printf("字母A的散列码是:%h %n", 'A');		//字母A的散列码是:41

    }

构造方法

    public static void main(String[] args) throws UnsupportedEncodingException {

//        String()
//        初始化新创建的 String对象,使其表示空字符序列。
        String a1 = new String();
        System.out.println(a1);//null

//        String(byte[] bytes)
//        通过使用平台的默认字符集解码指定的字节数组来构造新的 String 。
        byte[] chars= {'a','b'};
        String a2 = new String(chars);//ab

//        String(byte[] bytes, Charset charset)
//        构造一个新的String由指定用指定的字节的数组解码charset 。
        byte[] charss= {'a','b'};
        String a3 = new String(charss, "UTF-8");

//        String(byte[] bytes, int offset, int length)
//        通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String(offset要解码的第一个byte的索引,length要解码的byte数组的长度)
        byte[] chars4= {'f','r', 'a','b', 'c', 'd'};
        String a4 = new String(chars4, 3, 3);//bcd

//        String(byte[] bytes, int offset, int length, Charset charset)
//        构造一个新的String通过使用指定的指定字节子阵列解码charset 。
        byte[] chars5= {'a','b', 'c', 'd'};
        Charset ch5 = Charset.forName("UTF-8");
        String a5 = new String(chars5, 0, 3, ch5);//abc

//        String(byte[] bytes, int offset, int length, String charsetName)
//        构造一个新的 String通过使用指定的字符集解码指定的字节子阵列。
        byte[] chars6= {'a','b', 'c', 'd'};
        String a6 = new String(chars6, 0, 3, "UTF-8");//abc

//        String(byte[] bytes, String charsetName)
//        构造一个新的String由指定用指定的字节的数组解码charset 。
        byte[] chars7 = {'a','b', 'c', 'd'};
        String a7 = new String(chars7,"UTF-8");//abcd

//        String(char[] value)
//        分配一个新的 String ,以便它表示当前包含在字符数组参数中的字符序列。
        char[] bat1 = {'a','b', 'c', 'd'};
        String a8 = new String(bat1);//abcd

//        String(char[] value, int offset, int count)
//        分配一个新的 String ,其中包含字符数组参数的子阵列中的字符。(offset要解码的第一个byte的索引,count要解码的char的个数)
        char[] bat9 = {'e', 'r','a','b', 'c', 'd'};
        String a9 = new String(bat9,3,2);//bcd

//        String(int[] codePoints, int offset, int count)
//        分配一个新的 String ,其中包含 Unicode code point数组参数的子阵列中的 字符 。
        int[] int10 = {'a','b', 'c', 'd', 'e'};
        String a10 = new String(int10,1,3);//bcd

//        String(String original)
//        初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本
        String a11 = new String("asd");//asd

//        String(StringBuffer buffer)
//        分配一个新的字符串,其中包含当前包含在字符串缓冲区参数中的字符序列。
        StringBuffer bf = new StringBuffer();
        bf.append("asd");
        String a12 = new String(bf);//asd

//        String(StringBuilder builder)
//        分配一个新的字符串,其中包含当前包含在字符串构建器参数中的字符序列。
        StringBuilder bu = new StringBuilder();
        bu.append("asd");
        String a13 = new String(bu);//asd

    }

案例

    public static void main(String[] args) {
//        charAt(int index)
//        返回 char指定索引处的值
        String a = "abcd";
        char a1 = a.charAt(1);
        System.out.println(a1);//b

//        codePointAt(int index)
//        返回指定索引处的字符(Unicode代码点)
        IntStream a2 = a.chars();
        int [] a22 = a2.toArray();//{a,b,c,d}

//        compareTo(String anotherString)
//        按字典顺序比较两个字符串
        String a3 = "abcd";
        String a33 = "abcd";
        int a333 = a3.compareTo(a33);//0

//        compareToIgnoreCase(String str)
//        按字典顺序比较两个字符串,忽略病例差异(忽略大小写)
        String a4 = "abcd";
        String a44 = "aBcd";
        int a444 = a3.compareToIgnoreCase(a44);//0

//        concat(String str)
//        将指定的字符串连接到该字符串的末尾。
        String a5 = "asd";
        System.out.println(a5.concat("!!!"));//asd!!!

//        contains(CharSequence s)
//        当且仅当此字符串包含指定的char值序列时才返回true
        String a6 = "asd";
        System.out.println(a6.contains("s"));//true

//        contentEquals(CharSequence cs)
//        将此字符串与指定的CharSequence进行 CharSequence
        String a7 = "asd";
        System.out.println(a7.contentEquals("asd"));//true

//        contentEquals(StringBuffer sb)
//        将此字符串与指定的StringBuffer进行 StringBuffer
        String a8 = "asd";
        StringBuilder a888 = new StringBuilder();
        a888.append("ssd");
        System.out.println(a8.contentEquals(a888));//true

//        endsWith(String suffix)
//        测试此字符串是否以指定的后缀结尾
        String a9 = "asd";
        System.out.println(a9.endsWith("d"));//true

//        equals(Object anObject)
//        将此字符串与指定对象进行比较
        String b1 = "asd";
        System.out.println(b1.equals("asd"));//true

//        equalsIgnoreCase(String anotherString)
//        将此 String与其他 String比较,忽略案例注意事项(忽略大小写)
        String b2 = "asd";
        System.out.println(b2.equalsIgnoreCase("asd"));//true

//        format(Locale l, String format, Object... args)
//        使用指定的区域设置,格式字符串和参数返回格式化的字符串
        System.out.println(String.format("字符串:%s整数",2));//字符串:2整数

//        format(String format, Object... args)
//        使用指定的格式字符串和参数返回格式化的字符串
        System.out.println(String.format(Locale.CHINA,"字符串:%s整数",2));//字符串:2整数

//        getBytes()
//        使用平台的默认字符集将此 String编码为字节序列,将结果存储到新的字节数组中
        String b3 = "asd";
        byte [] b33 = b3.getBytes();//{a,s,d}

//        hashCode()
//        返回此字符串的哈希码
        String b4 = "卢大宝";
        System.out.println(b4.hashCode());//21244472

//        indexOf(int ch)
//        返回指定字符第一次出现的字符串内的索引
        String b5 = "asdqweqws";
        System.out.println(b5.indexOf("s"));//1

//        indexOf(int ch, int fromIndex)
//        返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索
        String b6 = "asdqweqws";
        System.out.println(b6.indexOf("s",2));//8

//        indexOf(String str)
//        返回指定子字符串第一次出现的字符串内的索引
        String b7 = "wedqweqws";
        System.out.println(b7.indexOf("we"));//0

//        indexOf(String str, int fromIndex)
//        返回指定子串的第一次出现的字符串中的索引,从指定的索引开始
        String b8 = "wedqweqws";
        System.out.println(b8.indexOf("we",1));//4

//        isEmpty()
//        返回 true如果,且仅当 length()为 0
        String b9 = "";
        System.out.println(b9.isEmpty());//true

//        String	join(CharSequence delimiter, CharSequence... elements)
//        返回一个新的字符串,由 CharSequence elements的副本组成,并附有指定的delimiter的 delimiter
        StringBuilder c11 = new StringBuilder();
        StringBuilder c111 = new StringBuilder();
        c11.append("-");
        c111.append("123");
        String c1 = String.join(c11, c111,c111);//123-123
        System.out.println(c1);

        String[] arrStr=new String[]{"a","b","c"};
        System.out.println(String.join("-", arrStr));

//        lastIndexOf(int ch)
//        返回指定字符的最后一次出现的字符串中的索引
        String c2 = "asdasd";
        System.out.println(c2.lastIndexOf("sd"));//4

//        lastIndexOf(int ch, int fromIndex)
//        返回指定字符的最后一次出现的字符串中的索引,从指定的索引开始向后搜索
        String c3 = "asdasd";
        System.out.println(c3.lastIndexOf("sd",4));//4

//        length()
//        返回此字符串的长度
        String c4 = "asd";
        System.out.println(c4.length());//3

//        replace(char oldChar, char newChar)
//        返回从替换所有出现的导致一个字符串 oldChar在此字符串 newChar 。
        String c7 = "asdasd";
        System.out.println(c7.replace("da","X"));//asXsd

//        replaceAll(String regex, String replacement)
//        用给定的替换替换与给定的 regular expression匹配的此字符串的每个子字符串
        String c8 = "asd5a4s3d";
        System.out.println(c8.replaceAll("\\d","-"));//asd-a-s-d

//        split(String regex)
//        将此字符串分割为给定的 regular expression的匹配
        String c9 = "a,s,d5a4s,3d";
        System.out.println(c9.split(","));//{a,s,d5a4s,3d}

//        startsWith(String prefix)
//        测试此字符串是否以指定的前缀开头
        String d1 = "asdqwe";
        System.out.println(d1.startsWith("as"));//true

//        startsWith(String prefix, int toffset)
//        测试在指定索引处开始的此字符串的子字符串是否以指定的前缀开头
        String d2 = "asdqwe";
        System.out.println(d2.startsWith("as",1));//fasle

//        subSequence(int beginIndex, int endIndex)
//        返回一个字符序列,该序列是该序列的子序列
        StringBuilder d33 = new StringBuilder();
        d33.append("asdqwe");
        System.out.println(d33.subSequence(1,2));//s(截取的字符串中不包括结束索引对应的字符)

//        substring(int beginIndex)
//        返回一个字符串,该字符串是此字符串的子字符串
        String d4 = "asdqwe";
        System.out.println(d4.substring(1));//sdqwe

//        substring(int beginIndex, int endIndex)
//        返回一个字符串,该字符串是此字符串的子字符串
        String d5 = "asdqwe";
        System.out.println(d5.substring(1,2));//s(截取的字符串中不包括结束索引对应的字符)

//        toCharArray()
//        将此字符串转换为新的字符数组。
        String d66 = "asd";
        char [] d6 = d66.toCharArray();//{a,s,d}

//        toLowerCase()
//        将所有在此字符 String使用默认语言环境的规则,以小写。
        String d7 = "ASD";
        System.out.println(d7.toLowerCase());//asd

//        toString()
//        此对象(已经是字符串!)本身已被返回。
        String d8 = "asd".toString();//asd

//        toUpperCase()
//        将所有在此字符 String使用默认语言环境的规则大写
        String d9 = "asd";
        System.out.println(d9.toUpperCase());//ASD

//        trim()
//        返回一个字符串,其值为此字符串,并删除任何前导和尾随空格
        String e1 = " asd ";
        System.out.println(e1.trim());//asd

//        valueOf(int i)
//        返回 int参数的字符串 int形式
        int e2 = 123;
        System.out.println(Integer.valueOf(e2));//123
    }

 

JDK
charcharAt(int index)

返回 char指定索引处的值

intcodePointAt(int index)

返回指定索引处的字符(Unicode代码点)

intcodePointBefore(int index)

返回指定索引之前的字符(Unicode代码点)

intcodePointCount(int beginIndex, int endIndex)

返回此 String指定文本范围内的Unicode代码点数

intcompareTo(String anotherString)

按字典顺序比较两个字符串

intcompareToIgnoreCase(String str)

按字典顺序比较两个字符串,忽略病例差异

Stringconcat(String str)

将指定的字符串连接到该字符串的末尾。

booleancontains(CharSequence s)

当且仅当此字符串包含指定的char值序列时才返回true

booleancontentEquals(CharSequence cs)

将此字符串与指定的CharSequence进行 CharSequence 

booleancontentEquals(StringBuffer sb)

将此字符串与指定的StringBuffer进行 StringBuffer 

static StringcopyValueOf(char[] data)

相当于 valueOf(char[]) 

static StringcopyValueOf(char[] data, int offset, int count)

相当于 valueOf(char[], int, int) 

booleanendsWith(String suffix)

测试此字符串是否以指定的后缀结尾

booleanequals(Object anObject)

将此字符串与指定对象进行比较

booleanequalsIgnoreCase(String anotherString)

将此 String与其他 String比较,忽略案例注意事项

static Stringformat(Locale l, String format, Object... args)

使用指定的区域设置,格式字符串和参数返回格式化的字符串

static Stringformat(String format, Object... args)

使用指定的格式字符串和参数返回格式化的字符串

byte[]getBytes()

使用平台的默认字符集将此 String编码为字节序列,将结果存储到新的字节数组中

byte[]getBytes(Charset charset)

使用给定的charset将该String编码为字节序列,将结果存储到新的字节数组中

voidgetBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)已弃用

此方法无法将字符正确转换为字节。 从JDK 1.1开始,首选的方法是通过getBytes()方法,该方法使用平台的默认字符集

byte[]getBytes(String charsetName)

使用命名的字符集将此 String编码为字节序列,将结果存储到新的字节数组中

voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

将此字符串中的字符复制到目标字符数组中

inthashCode()

返回此字符串的哈希码

intindexOf(int ch)

返回指定字符第一次出现的字符串内的索引

intindexOf(int ch, int fromIndex)

返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索

intindexOf(String str)

返回指定子字符串第一次出现的字符串内的索引

intindexOf(String str, int fromIndex)

返回指定子串的第一次出现的字符串中的索引,从指定的索引开始

Stringintern()

返回字符串对象的规范表示

booleanisEmpty()

返回 true如果,且仅当 length()0 

static Stringjoin(CharSequence delimiter, CharSequence... elements)

返回一个新的字符串,由 CharSequence elements的副本组成,并附有指定的delimiter的 delimiter 

static Stringjoin(CharSequence delimiter, Iterable<? extends CharSequence> elements)

返回一个新 String的副本组成 CharSequence elements与指定的副本一起加入 delimiter 

intlastIndexOf(int ch)

返回指定字符的最后一次出现的字符串中的索引

intlastIndexOf(int ch, int fromIndex)

返回指定字符的最后一次出现的字符串中的索引,从指定的索引开始向后搜索

intlastIndexOf(String str)

返回指定子字符串最后一次出现的字符串中的索引

intlastIndexOf(String str, int fromIndex)

返回指定子字符串的最后一次出现的字符串中的索引,从指定索引开始向后搜索

intlength()

返回此字符串的长度

booleanmatches(String regex)

告诉这个字符串是否匹配给定的 regular expression 

intoffsetByCodePoints(int index, int codePointOffset)

返回此 String内的指数,与 index codePointOffset代码点

booleanregionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)

测试两个字符串区域是否相等

booleanregionMatches(int toffset, String other, int ooffset, int len)

测试两个字符串区域是否相等

Stringreplace(char oldChar, char newChar)

返回从替换所有出现的导致一个字符串 oldChar在此字符串 newChar

Stringreplace(CharSequence target, CharSequence replacement)

将与字面目标序列匹配的字符串的每个子字符串替换为指定的字面替换序列

StringreplaceAll(String regex, String replacement)

用给定的替换替换与给定的 regular expression匹配的此字符串的每个子字符串

StringreplaceFirst(String regex, String replacement)

用给定的替换替换与给定的 regular expression匹配的此字符串的第一个子字符串

String[]split(String regex)

将此字符串分割为给定的 regular expression的匹配

String[]split(String regex, int limit)

将这个字符串拆分为给定的 regular expression的匹配

booleanstartsWith(String prefix)

测试此字符串是否以指定的前缀开头

booleanstartsWith(String prefix, int toffset)

测试在指定索引处开始的此字符串的子字符串是否以指定的前缀开头

CharSequencesubSequence(int beginIndex, int endIndex)

返回一个字符序列,该序列是该序列的子序列

Stringsubstring(int beginIndex)

返回一个字符串,该字符串是此字符串的子字符串

Stringsubstring(int beginIndex, int endIndex)

返回一个字符串,该字符串是此字符串的子字符串

char[]toCharArray()

将此字符串转换为新的字符数组。

StringtoLowerCase()

将所有在此字符 String使用默认语言环境的规则,以小写。

StringtoLowerCase(Locale locale)

将所有在此字符 String ,以降低使用给定的规则情况下 Locale

StringtoString()

此对象(已经是字符串!)本身已被返回。

StringtoUpperCase()

将所有在此字符 String使用默认语言环境的规则大写

StringtoUpperCase(Locale locale)

将所有在此字符 String使用给定的规则,大写 Locale 

Stringtrim()

返回一个字符串,其值为此字符串,并删除任何前导和尾随空格

static StringvalueOf(boolean b)

返回 boolean参数的字符串 boolean形式

static StringvalueOf(char c)

返回 char参数的字符串 char形式

static StringvalueOf(char[] data)

返回 char数组参数的字符串 char形式。

static StringvalueOf(char[] data, int offset, int count)

返回 char数组参数的特定子阵列的字符串 char形式

static StringvalueOf(double d)

返回 double参数的字符串 double形式

static StringvalueOf(float f)

返回 float参数的字符串 float形式

static StringvalueOf(int i)

返回 int参数的字符串 int形式

static StringvalueOf(long l)

返回 long参数的字符串 long形式

static StringvalueOf(Object obj)

返回 Object参数的字符串 Object形式

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值