Java的String和StringBuilder

Java字符串与StringBuilder详解
本文详细介绍了Java中String和StringBuilder的使用方法,包括创建、常用方法、字符串拼接及性能对比,强调了字符串不可变性和StringBuilder的灵活性,适用于Java初学者和进阶者。

一.String

  1.创建String对象的方法:

    String s1="haha";

    String s2=new String();

    String s3=new String("haha");

  2.String常用方法:

 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 比较。
 booleancontentEquals(StringBuffer sb) 
          将此字符串与指定的 StringBuffer 比较。
static StringcopyValueOf(char[] data) 
          返回指定数组中表示该字符序列的 String。
static StringcopyValueOf(char[] data, int offset, int count) 
          返回指定数组中表示该字符序列的 String。
 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 序列,并将结果存储到一个新的 byte 数组中。
 byte[]getBytes(Charset charset) 
          使用给定的 charset 将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。
 voidgetBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) 
          已过时。 该方法无法将字符正确转换为字节。从 JDK 1.1 起,完成该转换的首选方法是通过 getBytes() 方法,该方法使用平台的默认字符集。
 byte[]getBytes(String charsetName) 
          使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
 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() 
          当且仅当 length() 为 0 时返回 true
 intlastIndexOf(int ch) 
          返回指定字符在此字符串中最后一次出现处的索引。
 intlastIndexOf(int ch, int fromIndex) 
          返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
 intlastIndexOf(String str) 
          返回指定子字符串在此字符串中最右边出现处的索引。
 intlastIndexOf(String str, int fromIndex) 
          返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
 intlength() 
          返回此字符串的长度。
 booleanmatches(String regex) 
          告知此字符串是否匹配给定的正则表达式。
 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) 
          返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
 Stringreplace(CharSequence target, CharSequence replacement) 
          使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
 StringreplaceAll(String regex, String replacement) 
          使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
 StringreplaceFirst(String regex, String replacement) 
          使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。
 String[]split(String regex) 
          根据给定正则表达式的匹配拆分此字符串。
 String[]split(String regex, int limit) 
          根据匹配给定的正则表达式来拆分此字符串。
 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) 
          使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。
 StringtoString() 
          返回此对象本身(它已经是一个字符串!)。
 StringtoUpperCase() 
          使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
 StringtoUpperCase(Locale locale) 
          使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。
 Stringtrim() 
          返回字符串的副本,忽略前导空白和尾部空白。
static StringvalueOf(boolean b) 
          返回 boolean 参数的字符串表示形式。
 1 package Strings;
 2 
 3 public class StringTest1 {
 4 
 5     public static void main(String[] args) {
 6 
 7         String str="I am a student!";
 8         //求字符串长度
 9         System.out.println("str长度为:"+str.length());
10         //求某个位置的字符
11         System.out.println("位置为7的字符为:"+str.charAt(7));
12         //求子字符串
13         System.out.println("第7到15个字符组成的子字符串为:"+str.substring(7, 14));
14         System.out.println("第7到15个字符组成的子字符串为:"+str.substring(7));
15         //求字符在字符串中第一次出现的位置
16         System.out.println("第一次出现a的位置:"+str.indexOf("a"));
17         //求字串am在字符串中第一次出现的位置
18         System.out.println("第一次出现am的位置:"+str.indexOf("am"));
19 
20         //求字符在字符串中最后一次出现的位置
21         System.out.println("最后次出现a的位置:"+str.lastIndexOf("a"));
22         //求字串am在字符串中最后一次出现的位置
23         System.out.println("最后一次出现am的位置:"+str.indexOf("am"));
24         //。。。。。。。。
25 
26 
27     }
28 
29 }
View Code

   3.equals和==的区别:

    equals是判断内容是不是相同,==是判断内存地址是不是相同的。(相当于Python中的==【equals】和is【is】)。

 1 package Strings;
 2 
 3 public class StringTEst3 {
 4 
 5     public static void main(String[] args) {
 6 
 7         String str1="hello";
 8         String str2="hello";
 9         String str3=new String("hello");
10         System.out.println("str1和str2的内容相等:"+(str1.equals(str2)));
11         System.out.println("str1和str3的内容相等:"+(str1.equals(str3)));
12         /*在str1创建时,会在栈中生成一个变量str1,并在常量池中生成"hello"并将str1指向"hello"
13             ,然后当str2创建时,会在栈中生成str2,然后发现“hello”已经存在变量池中,就会把str2也指向hello
14             所以str1和str2的地址是一样的
15         */
16         System.out.println("str1和str2的内存地址相等:"+(str1==str2));
17         System.out.println("str1和str3的内存地址相等:"+(str1==str3));
18 
19     }
20 
21 }
22 /*
23  * 输出:
24  *     str1和str2的内容相等:true
25     str1和str3的内容相等:true
26     str1和str2的内存地址相等:true
27     str1和str3的内存地址相等:false
28     */
View Code

   4.字符串的不可变性:

    String对象一旦创建,则不能修改,是不可变的,所谓的修改其实是创建了新的对象,所指向的空间不变。

 1 package Strings;
 2 
 3 public class StringTest4 {
 4     public static void main(String[] args){
 5         String str1="hello";
 6         str1=str1+",haha";
 7         //创建了新的对象,str1不在指向"hello",而是指向"hello,haha"
 8         System.out.println(str1);
 9     }
10 
11 }
View Code

 二.StringBuilder

  1.String和StringBuilder的区别:

    String具有不可变性,而StringBuilder不具备。当频繁操作字符串时使用StringBuilder。

  2.StringBuilder和StringBuffer:

    两者基本相似,但StringBuffer是线程安全的,StringBuilder则没有,所以性能略高(推荐使用,因为大多数情况下处理字符串都是单线程的)。

  3.StringBuilder:

构造方法摘要
StringBuilder() 
          构造一个不带任何字符的字符串生成器,其初始容量为 16 个字符。
StringBuilder(CharSequence seq) 
          构造一个字符串生成器,它包含与指定的 CharSequence 相同的字符。
StringBuilder(int capacity) 
          构造一个不带任何字符的字符串生成器,其初始容量由 capacity 参数指定。
StringBuilder(String str) 
          构造一个字符串生成器,并初始化为指定的字符串内容。

 

方法摘要
 StringBuilderappend(boolean b) 
          将 boolean 参数的字符串表示形式追加到序列。
 StringBuilderappend(char c) 
          将 char 参数的字符串表示形式追加到此序列。
 StringBuilderappend(char[] str) 
          将 char 数组参数的字符串表示形式追加到此序列。
 StringBuilderappend(char[] str, int offset, int len) 
          将 char 数组参数的子数组的字符串表示形式追加到此序列。
 StringBuilderappend(CharSequence s) 
          向此 Appendable 追加到指定的字符序列。
 StringBuilderappend(CharSequence s, int start, int end) 
          将指定 CharSequence 的子序列追加到此序列。
 StringBuilderappend(double d) 
          将 double 参数的字符串表示形式追加到此序列。
 StringBuilderappend(float f) 
          将 float 参数的字符串表示形式追加到此序列。
 StringBuilderappend(int i) 
          将 int 参数的字符串表示形式追加到此序列。
 StringBuilderappend(long lng) 
          将 long 参数的字符串表示形式追加到此序列。
 StringBuilderappend(Object obj) 
          追加 Object 参数的字符串表示形式。
 StringBuilderappend(String str) 
          将指定的字符串追加到此字符序列。
 StringBuilderappend(StringBuffer sb) 
          将指定的 StringBuffer 追加到此序列。
 StringBuilderappendCodePoint(int codePoint) 
          将 codePoint 参数的字符串表示形式追加到此序列。
 intcapacity() 
          返回当前容量。
 charcharAt(int index) 
          返回此序列中指定索引处的 char 值。
 intcodePointAt(int index) 
          返回指定索引处的字符(统一代码点)。
 intcodePointBefore(int index) 
          返回指定索引前的字符(统一代码点)。
 intcodePointCount(int beginIndex, int endIndex) 
          返回此序列指定文本范围内的统一代码点。
 StringBuilderdelete(int start, int end) 
          移除此序列的子字符串中的字符。
 StringBuilderdeleteCharAt(int index) 
          移除此序列指定位置上的 char
 voidensureCapacity(int minimumCapacity) 
          确保容量至少等于指定的最小值。
 voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 
          将字符从此序列复制到目标字符数组 dst
 intindexOf(String str) 
          返回第一次出现的指定子字符串在该字符串中的索引。
 intindexOf(String str, int fromIndex) 
          从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。
 StringBuilderinsert(int offset, boolean b) 
          将 boolean 参数的字符串表示形式插入此序列中。
 StringBuilderinsert(int offset, char c) 
          将 char 参数的字符串表示形式插入此序列中。
 StringBuilderinsert(int offset, char[] str) 
          将 char 数组参数的字符串表示形式插入此序列中。
 StringBuilderinsert(int index, char[] str, int offset, int len) 
          将数组参数 str 子数组的字符串表示形式插入此序列中。
 StringBuilderinsert(int dstOffset, CharSequence s) 
          将指定 CharSequence 插入此序列中。
 StringBuilderinsert(int dstOffset, CharSequence s, int start, int end) 
          将指定 CharSequence 的子序列插入此序列中。
 StringBuilderinsert(int offset, double d) 
          将 double 参数的字符串表示形式插入此序列中。
 StringBuilderinsert(int offset, float f) 
          将 float 参数的字符串表示形式插入此序列中。
 StringBuilderinsert(int offset, int i) 
          将 int 参数的字符串表示形式插入此序列中。
 StringBuilderinsert(int offset, long l) 
          将 long 参数的字符串表示形式插入此序列中。
 StringBuilderinsert(int offset, Object obj) 
          将 Object 参数的字符串表示形式插入此字符序列中。
 StringBuilderinsert(int offset, String str) 
          将字符串插入此字符序列中。
 intlastIndexOf(String str) 
          返回最右边出现的指定子字符串在此字符串中的索引。
 intlastIndexOf(String str, int fromIndex) 
          返回最后一次出现的指定子字符串在此字符串中的索引。
 intlength() 
          返回长度(字符数)。
 intoffsetByCodePoints(int index, int codePointOffset) 
          返回此序列中的一个索引,该索引是从给定 index 偏移 codePointOffset 个代码点后得到的。
 StringBuilderreplace(int start, int end, String str) 
          使用给定 String 中的字符替换此序列的子字符串中的字符。
 StringBuilderreverse() 
          将此字符序列用其反转形式取代。
 voidsetCharAt(int index, char ch) 
          将给定索引处的字符设置为 ch
 voidsetLength(int newLength) 
          设置字符序列的长度。
 CharSequencesubSequence(int start, int end) 
          返回一个新字符序列,该字符序列是此序列的子序列。
 Stringsubstring(int start) 
          返回一个新的 String,它包含此字符序列当前所包含字符的子序列。
 Stringsubstring(int start, int end) 
          返回一个新的 String,它包含此序列当前所包含字符的子序列。
 StringtoString() 
          返回此序列中数据的字符串表示形式。
 voidtrimToSize() 
          尝试减少用于字符序列的存储空间。

 

 1 package Strings;
 2 
 3 public class StringTest5 {
 4 
 5     public static void main(String[] args) {
 6         StringBuilder str=new StringBuilder("hello");
 7 //        str.append(",");
 8 //        str.append("world!");
 9 //        System.out.println("str="+str);
10         //添加字符串
11         System.out.println("str="+str.append(",").append("world!"));
12         //替换字符串
13         //1.先delete,后insert
14         System.out.println("str="+str.delete(6, 11).insert(6,"WORLD"));
15         //2.使用replece
16         System.out.println("str="+str.replace(6,11,"WORLD"));
17         //取出子字符串
18         System.out.println("str="+str.substring(0,2));
19 
20 
21     }
22 
23 }
View Code

 

转载于:https://www.cnblogs.com/lyq-biu/p/10778863.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值