String、StringBuilder与StringBuffer

本文深入探讨Java中String与StringBuilder的特性与应用,包括字符串不可变性、常量池机制、编码与长度获取、检索与截取方法、大小写转换等。同时,对比分析String与StringBuilder在性能上的差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

String

1.String是不可修饰变量

2.String常量池

3.内存编码及长度

(1)内存编码

(2)获取 String对象的长度length()

3.使用indexOf实现检索

4.使用subString获取子串

5.Trim

6.charAt

8. String toLpwerCase()和 String toUpperCase()

StringBuilder

StringBuffer与StringBuilder区别


String

1.String是不可修饰变量

String使用了final修饰,不能被继承

字符串一旦创建,对象永远无法改变但字符串引用可以重新赋值

2.String常量池

Java为了提高性能,静态字符串在常量池中创建并尽量用使用同一个对象,重用静态字符串;对于重复出现的字符串直接量,JVM会首选在常量池中查找,如果存在返回该对象

String是不变对象,即:字符串对象一旦创建,内容不可改变,若改变则必须创建新对象

public class StringDemo {

        public static void main(String[] args) {

                /**

                 * 凡是使用字面量形式创建字符串对象JVM都会将其

                 * 缓存在字符串常量池中(对你村中单独开辟的一段区域)

                 * 当再次使用该子面常量创建字符串时就会重用之前的对象

                 */

                s1 = s1+"!";

                System.out.println(s1);//123abc!

                System.out.println(s2);//123abc

                System.out.println(s1 == s2);//不再指向同一个对象

                System.out.println(s2 == s3);

               

                //new一个新的对象,不推荐这样创建字符串对象

                String s4 = new String ("123abc");

                System.out.println(s4);

                /**

                 * 虽然与s2内容分一样,但是不是同一个对象

                 * 所以s2与s4保存的地址不同,因此“==”比较是false

                 */

                System.out.println(s2 == s4);//false

                /*

                 * 字符串在比较时通常都是比较字符串内容,因此我们

                 * 应当用equals方法进行比较

                 */

                System.out.println(s2.equals(s4));//true

                /**

                 * 编译器有一个特性:

                 * 当一个计算表达式在编译期间就可以确定结果时,编译器

                 * 就会进行计算,并将结果编译到。class字节码文件中,比如:

               * 下面的代码会在编译期间被计算,因此字节码文件中为:

                 * string s5 = “123abc”;

                 * 因此虚拟机实际执行时看到的是上面这句,所以和s2重用对象了

                 */

                String s5 = "123"+"abc";

                System.out.println(s5);

                System.out.println(s2 == s5);//true

               

                String s = "123";

                String  s6 = s +"abc";

                System.out.println(s6);

                System.out.println(s2 == s6);//false

        }
}

3.内存编码及长度

(1)内存编码

采用Unicode编码,每个字符16位占用两个字节;任何一个字符
(无论中文还是英文)都算一个char字符长度,占用两个字节

(2)获取 String对象的长度length()

int length ()                            返回当前字符串的长度(字符个数)

public class LengthDemo {

 public static void main(String[] args) {

 String str = "我喜欢学java!";

 System.out.println(str.length());//9

}

}

3.使用indexOf实现检索

方法:

int indexOf(String str)//检索str第一次出现的位置若找不到则返回-1

int indexOf(String str ,int fromIndex)//从字符串的fromIndex位置开始检索

int lastIndexOf(String str,int from)//检索最后一个str的位置

public class IndexOfDemo {

 public static void main(String[] args) {

String str = "thinking  in our home";

  //检索in在str中第一次出现的位置

  int index = str.indexOf("in");

  System.out.println(index);

  //检索in在str中从第5个字符开始后的第一次出现的位置

  index = str.indexOf("in", 5);

  System.out.println(index);

  //检索in在str中最后一次出现的位置

  index = str.lastIndexOf("in");

  System.out.println(index);

}

}

4.使用subString获取子串

String substring (int start ,int end)

截取当前字符串中指定范围内大的字符串,两个参数为下标,注意在API中有一个特点:通常用两个数字表示范围是都是含头不含尾的

String substring (int start )若只有一个参数,则从start下标开始到字符串结尾的子字符串

public class SubStringDemo {

 public static void main(String[] args) {

 String str = "www.tedu.cn";

  //截取域名

  String sub = str.substring(4, 8);//含头不含尾

  System.out.println(sub);

  //只传入一个参数时,是从该位置到截取字符串末尾

  sub = str.substring(4);

  System.out.println(sub);

}

}

5.Trim

去掉前导后继的空字符

public class TrimDemo {

 public static void main(String[] args) {

 String str = "       hello    ";

  String trim = str.trim();

  System.out.println(trim);

 }

}

6.charAt

char charAt(int index)

返回当前字符串中指定位置的字符

public class CharAtDEMO {

 public static void main(String[] args) {

  String STR = "helloworld!";

  char c = STR.charAt(5);

  System.out.println(c);

  for (int i = 0; i < STR.length(); i++) {

   System.out.print(STR.charAt(i)+"\t");

  }

 }

}

7.startsWith和endsWith

boolean startsWith(String str)

boolean endsWith(String str)

判断当前字符串是否是以给定的内容开始或结束的

public class StartsWithDemo {

 public static void main(String[] args) {

 String str = "www.tedu.cn";

  boolean starts = str.startsWith("www");

  System.out.println(starts);

  boolean ends = str.endsWith(".cn");

  System.out.println(ends);

 }

}

8. String toLowerCase()和 String toUpperCase()

 将当前字符串中的英文部分转换为全大写或全小写

public class TouperCase {

 public static void main(String[] args) {

  String str = "你好Java";

  String  upper =  str.toUpperCase();

  System.out.println(upper);

  String lower = str.toLowerCase();

  System.out.println(lower);

 

  //验证码对比案例

  String code = "Ade34";

  String input = "adE34";

  //都转换为大写或小写

  String c = code.toUpperCase();

  String i = input.toUpperCase();

  if(c.equals(i)){//判断内容就可以忽略大小写了

   System.out.println("输入正确");

  }else{

   System.out.println("输入错误");

  }

  /*

   * 忽略大小写比较两个字符串内容

   */

  if(code.equalsIgnoreCase(input)){

   System.out.println("输入正确");

  }else{

   System.out.println("输入错误");

  }
 }

}

 随机验证码案例:点击进入

9.ValueOf

String提供了一组静态方法ValueOf
作用是将其他类型转换为String,比较常用的是将基本类型转换为String

public class ValueOfDemo {
 public static void main(String[] args) {
 
  double  d= 123.123;
  String str2 = String.valueOf(d);
  System.out.println(str2);
  
  str = a+"";
  System.out.println(str);
 }

}

StringBuilder

封装可变字符数组,创建对象后可以通过调用方法改变其封装的字符串

构造方法:

Public StringBuilder()

Public StringBuilder(String str)

StringBuilder返回值均为StringBuilder类,返回语句均为:return this

public class StringBuliderDemo {

 public static void main(String[] args) {

  String line = "好好学习java";

  //默认内部表示一个空字符串

 //StringBuilder builder = new StringBulider();

  //复制给定的字符串到StringBuilder内部

  StringBuilder builder = new StringBuilder(line);

 

  /*

   * 增:

   * 好好学习java

   * 好好学习java,为了找个好工作!

   *

   * append:追加 ,相当于字符串“+”操作

   *

   */

  builder.append(",为了找个好工作!");

  System.out.println(builder);

 

  /*

   * 改:

   * 好好学习java,为了找个好工作!

   * 好好学习java,就是为了改变世界!

   * replance:替换当前字符串指定范围内的内容

   *

   */

  builder.replace(9, 16, "就是为了改变世界");

  System.out.println(builder);

 

  /*

   * 删:

   * 好好学习java,就是为了改变世界!

   * 就是为了改变世界!

   * delete:删除指定范围内的字符串

   */

  builder.delete(0, 9);

  System.out.println(builder);

 

  /*

   * 插:

   * 就是为了改变世界!

   * 活着就是为了改变世界!

   * insert:将指定内容插入到当前字符串中指定位置

   */

  builder.insert(0, "活着");

  System.out.println(builder);

  //获取builder内部改完的字符串

  String s = builder.toString();

  System.out.println(s);

 }

}

StringBuffer与StringBuilder区别

StringBuffer:线程安全的,同步处理的,性能稍慢

StringBuilder:非线程安全的,并发处理的,性能稍快

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Healer_小振

感谢大佬的支持和鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值