String

String

1. 特性:

  1. 任意字符串都是String的对象

  2. 字符串的内容是不可改变的

  3. 相同内容的字符串地址是一样的

  4. final修饰,不可被继承

  5. 相同的两段字符串是有缓存的,重复使用的

2. 字符串常量池的变更历史

JDK1.7之前:
运行时常量池(包括字符串常量池)存放在方法区,此时 hotspot虚拟机对方法区的实现改为永久代

JDK1.7:
字符串常量池从方法区拿到了堆中
运行时常量池剩下的东西还在方法区,也就是Hotspot的永久代

JDK1.8:
hotspot移除了永久代,用元空间代替,这时候字符串常量池还在堆,运行时常量池还在方法区,只不过方法区的实现从永久代变成元空间

3. 常用方法

序号方法定义类型描述
1public String(char[] value)构造将一个字符数组变成字符串
2public String(char[] value,int offset,int count)构造将一个指定范围的字符数组围变成字符串
3public String(byte[] bytes)构造将一个字节数组变成字符串
4public String(byte[] bytes,int offset,int length) 构造将一个指定范围的字节数组变成字符串
5public char[] toCharArray()方法将字符串变成字符数组
6public char charAt(int index)方法从字符串中提取指定位置的字符
7public byte[] getBytes()方法将字符串变成字节数组
8public int length()方法取得字符串的长度
9public int indexOf(String str,int fromIndex)方法从指定位置查找指定字符串的位置
10public String trim()方法清除左右两端的空格
11public String substring(int begin,int end)方法截取指定范围的字符串
12public String[] split(String regex)方法按照指定的字符串对字符串进行拆分
13public String toUpperCase()方法将 字符串全部变成大写
14public String toLowerCase()方法将字符串全部变成小写
15public boolean startsWith(String prefix) 方法判断是否以指定字符串开头
16public boolean endWith(String suffix)方法判断是否以指定字符串结尾
17public boolean equals(String str)方法判断两个字符串的内容是否相同
18public boolean equalsIgnoreCase(String str)方法忽略大小写,判断两个字符串的内容是否相同
19public String repaceAll(String regex,String replacement)方法字符串 替换

3.1 字符串截取

示例

String text = "hello world";
System.out.println(text.substring(6,11));
结果:
world

3.2 按照指定的字符串拆分字符串

示例

 String text = "hello world";
String[] text1 = text.split("");
for(int i= 0;i<text1.length;i++){
      System.out.print(text1[i]);
 }
 结果:
 hello world
 

3.3 字符串的大小写转换

示例:

String text = "hello world";
 System.out.println(text.toUpperCase());
 String text1 = "HELLO WORLD";
 System.out.println(text1.toLowerCase());
结果:
HELLO WORLD
hello world

3.4 判断是否以指定的字符串开头或结尾

示例:

String text = "hello world";
        if(text.startsWith("hello")){
            System.out.println(text+"以hello开头");
        }
        String text1 = "HELLO WORLD";
        if(text1.endsWith("WORLD")){
            System.out.println(text1+"以WORLD结尾");
        }
        
结果:
hello world以hello开头
HELLO WORLD以WORLD结尾

3.5 将一个指定的字符串替换成其他的字符串

示例:

tring text = "hello world";
System.out.println(text.replaceAll("l","x"));//将l全部替换为x

结果:
hexxo worxd

4. 字符串拼接

text1 = text1+text2+text3
使用符号"+"进行字符串拼接时,内存里首先有	text1,text2,text3  3个String对象
执行这个拼接语句后, 先拼接text1+text2并开辟一块内存存放这个值,再将这个值与text3拼接.完成该语句功能,
此时内存中有 text1原来的值,  text1+text2 这两块内存垃圾. 
所以尽量避免使用String字符串拼接

StringBuffer类

1. 概念

使用String类的对象进行字符串拼接时,会浪费大量的内存,而StringBuffer类是java专门用于字符串拼接的类.StringBuilder是线程不安全的实现.StringBuffer是线程安全的实现

2. 常用方法

序号方法定义类型描述
1public StringBuffer()构造StringBuffer的构造方法
2public StringBuffer append(char c)方法在StringBuffer中提供了大量的追加操作,类似String的"+",这个方法可以添加char类型
3public StringBuffer append(String str)方法这个方法可以添加String类型
4public StringBuffer append(StringBuffer sb)方法这个方法可以添加StringBuffer类型
5public int indexOf(String str)方法查找指定字符串是否存在
6public int indexOf(String str,int fromIndex)方法从指定位置开始查找指定字符串是否存在
7public StringBuffer insert(int offset,String str)方法在指定位置处加上指定字符串
8public StringBuffer reverse()方法将内容反转保存
9public StringBuffer replace(int start,int end ,String str)方法指定内容替换
10public int length()方法求出内容长度
11public StingBuffer delete(int start,int end)方法删除指定范围内的字符串
12public String substring(int start)方法指定开始点进行字符串截取
13public String substring(int start,int end)方法指定范围进行字符串截取
14public String toString()方法将内容变为String类型
2.1 使用append方法进行字符串连接

示例:

StringBuffer sb = new StringBuffer("hello");
sb.append("world").append("!!!");
System.out.println(sb.toString());

结果:
	helloworld!!!
2.2 使用insert方法给StringBuffer对象添加内容

示例:

  StringBuffer sb = new StringBuffer("world");
  sb.insert(0,"hello");
  System.out.println(sb.toString());
  
  结果:
  helloworld
2.3 反转字符串

示例:

 StringBuffer sb = new StringBuffer("world");
        sb.insert(0,"hello");
        System.out.println(sb.toString());
        System.out.println( sb.reverse().toString());//reverse方法返回一个反转后的		                                                           StringBuffer对象
 
结果:
helloworld
dlrowolleh
2.4 替换指定范围的内容

示例:

  StringBuffer sb = new StringBuffer("world");
        sb.insert(0,"hello");
        System.out.println(sb.toString());
        sb.replace(5,10,"robot);//将world变为robot
        System.out.println(sb.toString());
        
结果:
helloworld
hellorobot
2.5 字符串截取

示例:

StringBuffer sb = new StringBuffer("world");
sb.insert(0,"hello");
System.out.println(sb.toString());
 String str = sb.substring(5,10);//截取world
System.out.println(str);

结果:
helloworld
world
2.6 删除指定范围字符串

示例

StringBuffer sb = new StringBuffer("world");
        sb.insert(0,"hello");
        System.out.println(sb.toString());
        System.out.println(sb.delete(5,10).toString());//删除world
        
结果:
helloworld
hello
2.7 查找指定内容是否存在

示例

StringBuffer sb = new StringBuffer("world");
        sb.insert(0,"hello");
        System.out.println(sb.toString());
        if(sb.indexOf("hello")== -1) {//找不到便返回-1
            System.out.println("该字符串不存在");
        }else{
            System.out.println("可以查找到指定的内容");
        }
        
 结果:
 可以查找到指定的内容
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值