常用类——String类

String

  • 字符串是常量,创建之后不可改变
  • 字符串字面值存储在字符串池中,可以共享
package common.common03;

public class Demo01 {
    public static void main(String[] args) {
        String name="hello";//hello存在方法池中
        name="zhangsan";//zhangsan赋值给name,给字符串赋值时,没有修改数据,而是重新开辟了一个空间
        String name2="zhangsan";

        //字符串的另一种创建方式
        String str=new String("11111");
    }
}
  • String s="hello"//产生一个对象,字符串池中存储
    
  • String str =new String("hello");//产生两个对象,堆池中各一个
    

常用方法

  • public int length();返回字符串长度
    
  • public char charAt(int index);根据下标获取字符
    
  • public boolean contains(String str);判断当前字符串是否含有str
    
//字符串方法的使用
//1.length()返回字符串长度
//2。charAt(int index)返回某个位置的字符
//3.contains(String str) 判断是否包含某个字符串

String content="1223449499";
System.out.println(content.length());
System.out.println(content.charAt(0));
System.out.println(content.contains("33"));
System.out.println(content.contains("12"));
  • public char[] toCharArray():将字符串转化
    
  • public int indexOf(String str);查找str首次出现的下标,存在,则返回该下标,不存在,则返回-1
    
  • public int lastIndexOf(String str);查找字符串在当前字符串中最后一次出现的下标
    
//4.toCharArray()返回字符串对应的数组
//5.indexOf()返回字符串首次出现的位置
//6.lastIndexOf()返回字符串最后一次出现的位置
System.out.println(Arrays.toString(content.toCharArray()));
System.out.println(content.indexOf(2));
System.out.println(content.lastIndexOf(3));
  • public String trim();去掉字符串前后的空格
    
  • public String toUPperCase();将大写改成小写
    
  • public boolean endWith(String str) 判断字符串是否以str结尾
    
//7.trim()去掉字符串前后的空格
//8.toUpperCase() 把小写转成大写 toLowerCase()转成小写
//9.endWith(String str) 是否以字符串结尾 startWith(str) 是否以字符串开头
String content2="  hello  world  ";
System.out.println(content2.trim());
System.out.println(content2.toUpperCase());

System.out.println(content2.endsWith(" "))
  • public String replace(char oldChar,char newChar);将旧字符串替换成新字符串
    
  • public String[] split(String str)根据str做拆分 
    
//10.replace();用新的字符或字符串替换旧的字符和字符串
//11.split();对字符串进行拆分
System.out.println(content.replace("9","3"));//用3替换9
String say="java is the best programing language ,java";
String[] arr=say.split("[ ,]");
System.out.println(arr.length);
for(String string:arr){
    System.out.println(string);
}

//补充:equals() compare()//比较大小
String s1="hello";
String s2="hello";
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));//忽略大小写比较

String s3="abc";
String s4="xyz";
System.out.println(s3.compareTo(s4));//s3-s4

可变字符串

  • StringBuffer:运行速率慢,线程安全
  • StringBuilder:运行效率快,线程不安全
//StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
//1.append();追加
sb.append("java是一门很好的语言");
System.out.println(sb.toString());
sb.append("java真香");
System.out.println(sb.toString());
sb.append("java真不糗");
System.out.println(sb.toString());
//insert() 添加
sb.insert(0,"我在最前面");
System.out.println(sb.toString());
//replace()
sb.replace(0,5,"hello");
System.out.println(sb.toString());
//delete()删除
sb.delete(0,5);
System.out.println(sb.toString());
//清空
sb.delete(0,sb.length());
System.out.println(sb.toString());
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值