Java--模拟实现字符串的基本操作

  1. equals
    请解释String类中两种对象实例化的区别
    1. 直接赋值:只会开辟一块堆内存空间,并且该字符串对象可以自动保存在对象池中以供下次使用。
      string str = “hello”;
    2. 构造方法:会开辟两块堆内存空间,其中一块成为垃圾空间,不会自动保存在对象池中,可以使用intern()方法手工入池。
      string str = new string(“hello”)
      int a = 10;
      int c = 10;
      String str1 = new String("hello");
      String str2 = new String("hello");
      String str3 = "cat";
      String str4 = "cat";
      System.out.println(a == c);//true
      System.out.println(str1 == str2);//false  == 在Java中比较的是地址
      System.out.println(str3 == str4);//true
      System.out.println("hello".equals(str1));  //equals比较的是字符串    
public class test {
    public static void main(String[] args) {
         String str1 = new String("hello");//不在字符串常量池
         String str2 = "hello";
         System.out.println(str1==str2);//false
         String str3 = new String("hello").intern();//手动加入常量池
        String str4 = "hello";
        System.out.println(str3==str4);//true
       
}

2 .compareTo

  • 在String类中compareTo()方法是一个非常重要的方法,该方法返回一个整型,该数据会根据大小关系返回三类内容:

  • 相等返回0;

  • 小于返回小于0的数

  • 大于返回大于0的数

System.out.println("A".compareTo("a")); // -32
System.out.println("a".compareTo("A")); // 32
System.out.println("A".compareTo("A")); // 0
System.out.println("AB".compareTo("AC")); // -1
System.out.println("刘".compareTo("杨"));//-5456
  1. toCharArray
String str = "helloworld" ;
// 将字符串变为字符数组
char[] data = str.toCharArray() ;//toCharArray转换为字符数组
for (int i = 0; i < data.length; i++) {
    System.out.print(data[i]+" ");
}
// 字符数组转为字符串
System.out.println(new String(data)); // 全部转换  hellowworld
System.out.println(new String(data,5,5)); // 部分转换  hello
  1. contains
    可用于字符串查找,查看子字符串是否在其中
String str = "hello";
String str2 = "helloworld" ; 
System.out.println(str2.contains("world")); // true 
System.out.println(str.contains("world")); // false 
  1. indexOf
    现在基本都是用contains()方法完成。
    使用indexOf()需要注意的是,如果内容重复,它只能返回查找的第一个位置
String str = "helloworld" ;
System.out.println(str.indexOf("world")); // 5,w开始的索引
System.out.println(str.indexOf("bit")); // -1,没有查到
if (str.indexOf("hello") != -1) {
    System.out.println("可以查到指定字符串!");
}
  1. lastIndexOf
    查询返回的是下标
String str = "helloworld" ;
System.out.println(str.indexOf("l")); // 2
System.out.println(str.indexOf("l",5)); // 8  从指定下标开始查询
System.out.println(str.lastIndexOf("l")); // 8  从后开始查询
  1. replaceAll 8. replaceFirst
    注意事项: 由于字符串是不可变对象 替换不修改当前字符串 而是重新创建一个新的字符串
String str = "helloworld" ;
System.out.println(str.replaceAll("l", "_")); replaceAll 全都替换  he__owor_d
System.out.println(str.replaceFirst("l", "_"));replaceFirst 替换首个 he_loworld
  1. split
    字符串的拆分
String str = "hello world hello bit" ;
String[] result = str.split(" ") ; // 按照空格拆分
for(String s: result) {//循环
    System.out.println(s);
}
//结果
hello
world
hello
bit

注意事项:

  1. 字符"|","*","+“都得加上转义字符,前面加上”".
  2. 而如果是"",那么就得写成"\".
  3. 如果一个字符串中有多个分隔符,可以用"|"作为连字符.
String str = "192.168.1.1" ;
String[] result = str.split("\\.") ;
for(String s: result) {
    System.out.println(s);
}
多次拆分
String str = "name=zhangsan&age=18" ; 
String[] result = str.split("&") ; 
for (int i = 0; i < result.length; i++) { 
    String[] temp = result[i].split("=") ; 
    System.out.println(temp[0]+" = "+temp[1]); 
    }
//运行结果
name = zhangsan
age = 18    
  1. subString
    从一个完整的字符串之中截取出部分内容。
    注意:
    1. 索引从0开始
    2. 注意前闭后开区间的写法, substring(0, 5) 表示包含 0 号下标的字符, 不包含 5 号下标
String str = "helloworld" ;
System.out.println(str.substring(5));
System.out.println(str.substring(0, 5));
//运行结果
world
hello
  1. trim
    去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等).
String str = " hello world " ;
System.out.println("["+str+"]");
System.out.println("["+str.trim()+"]");
//trim 会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等).
//运行结果
[ hello world ]
[hello world]
  1. isEmpty
    判断字符串是否为空
System.out.println("hello".isEmpty());
System.out.println("".isEmpty());
System.out.println(new String().isEmpty());
  1. length
    求字符串的长度
String str = " hello%$$%@#$%world 哈哈哈 " ; 
	System.out.println(str.length()); 
 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值