Java的String类

String类

  • 字符串是常量,创建之后不可改变。

  • 字符串字面值存储在字符串池中,可以共享。

  • String s = "hello"; 产生一个对象,存储在字符串池中。

  • String s = new String("hello"); 会产生两个对象,在堆和字符串池中各存储一个。


package com.qf.demo;

public class Demo3{
   public static void main(String[] args){
       String name = "hello"; //hello常量存储在字符串池中
       name = "zhagnsan"; //"zhangsan"赋值给了name变量,给字符串赋值时,并未修改数据,而是重新在方法区里开辟空间写入张三,将原来对hello的指向改为指向张三,然后hello变成垃圾值。
       String name2 = "zhangsan"; //程序在字符串池中找到张三,也把name2指向张三。
       
       //演示字符串的另外一种创建方式:new String();
       String str =new String("java是最好的编程语言");
       String str2 = new String("java");
       System.out.println(str == str2);
       System.out.println(str.equals(str2)); //这里String重写了equals方法,不再比较地址而是比较存储值。
  }
}

常用方法

  • public int length():返回字符串的长度。

  • public char charAt(int index):根据下标获取字符。

  • public boolean contains(String str):判断当前字符串中是否包含str。

  • public char[] toCharArray():将字符串转换成数组。

  • public int indexOf(String str):查找str首次出现的下标,存在则返回该下标,不存在则返回-1。

  • public int lastIndexOf(String str):查找字符串在当前字符串中最后一次出现的下标索引。

  • public String trim():去掉字符串前\后的空格。

  • public String toUpperCase():将小写转换成大写。

  • public boolean endWith(String str):判断字符串是否以str结尾。

  • pulbic String replace(char oldChar, char newChar):将旧字符串替换成新字符串。

  • public String[] split(String str):根据str做拆分。


System.out.println("*****字符串方法的使用1*****");
//1.length();返回字符串的长度
//2.charAt(int index);返回某个位置的字符
//3.contains(String str);判断是否包含某个子字符串
String content = "java no.1, java is the best.";
System.out.println(content.length());
System.out.println(content.charAt(content.length() - 1));
System.out.println(content.contains("java"));
System.out.println(content.contains("php"));

System.out.println("*****字符串方法的使用2*****");
//4.toCharArray();返回字符串对应的字符
//5.indexOf();返回子字符串首次出现的位置
//6.lastIndexOf();返回字符串最后一次出现的位置
System.out.println(Arrays.toString(content.toCharArray())); //直接打印content.toCharArray();打印出来的是它的位置。
System.out.println(content.indexOf("java"));
System.out.println(content.indexOf("java", 4)); //4代表从第5个位置开始向后找。
System.out.println(content.lastIndexOf("java"));

System.out.println("*****字符串方法的使用3*****");
//7.trim();去掉字符串前后的空格。
//8.toUpperCase(); //把小写转成大写 → toLowerCase();把大写转成小写。
//9.endWith(String str);判断是否以str结尾。 → startWith(String str);判断是否以str开头。
String content2 = "   Hello Word   ";
System.out.println(content2.trim());
System.out.println(content2.toUpperCase());
System.out.println(content2.toLowerCase());

String filename = "hello.java";
System.out.println(filename.endWith(".java"));
System.out.println(filename.startWith("hello"));

System.out.println("*****字符串方法的使用4*****");
//10.replace(char old, char new);用新的字符/字符串替换旧的字符/字符串。
//11.split();对旧的字符串进行拆分。
System.out.println(content.replace("java", "php"));
String say = "java is the best   programing language,java xiang";
String[] arr = say.split(" ");
//String[] arr = say.split("[ ,]"); //中括号表示选择字符,我可以用空格以及逗号。
//String[] arr = say.split("[ ,]+"); //额外增加个加号表示我的空格或者逗号可以出现一个或者多个。
System.out.println(arr.length());
for (String srt : arr){
   System.out.println(srt);
}

System.out.println("*****字符串方法的补充*****");
//补充两个方法
//11.equals();
//12.compareTo(); 比较大小:取从左往右第一个相异的字母在字典(编码)表里的顺序差值。
String s1 = "hello";
String s2 = "Hello";
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2)); //忽略大小写的比较
String s3 = "abc"; //a:97
String s4 = "xyz"; //x:120
System.out.println(s3.compareTo(s4));
String s5 = "abc";
String s6 = "abcxyz"; //这里不再比较位置,而是比较长度,做差值。
System.out.println(s5.compareTo(s6));

案例演示

  • 需求

    • 已知String str = "this is a text";

      1. 将 str 中的单词单独获取出来

      2. 将 str 中的 text 替换为 practice

      3. 在 text 前面插入一个 easy

      4. 将每个单词的首字母改为大写


package com.qf.demo;

public class Demo4{
   public static void main(String[] args){
       String str = "this is a text";
       
       //1.将 str 中的单词单独获取出来
       String[] arr = str.split(" ");
       for(String s : arr){
           System.out.println(s);        
      }
       
       //2.将 str 中的 text 替换为 practice
       String str2 = str.replace("text", "practice");
       System.out.println(str2);
       
       //3.在 text 前面插入一个 easy
       String str3 = str.replace("text", "easy text");
       System.out.println(str3);
       
       //4.将每个单词的首字母改为大写
       for(int i=0; i<arr; i++){
           char firstC = arr[i].charAt(0);
           //把第一个字符转为大写
           char UpperFirst = Character.toUpperCase(firstC);
           //把单词拼接起来
           String newArray = UpperFirst + arr[i].substring(1); //截取第二位开始的字符串
           System.out.println(newArray);
      }
  }
}

可变字符串

  • StringBuffer:可变长字符串,JDK1.0提供,运行效率慢,线程安全。可理解为 String 的增强类。

  • StringBuilder:可变长字符串,JDK5.0提供,运行效率快,线程不安全。JDK1.5后的单线程推荐使用。

  • 和 String 区别:

    • 效率比 String 要高

    • 比 String 更省内存


package com.qf.demo;

public class Demo5{
   public static void main(String[] args){
       StringBuffer sb = new StringBuffer();
       //StringBuilder sb = new StringBuffer();也是一样的
       
       //1.append();在字符串末端追加字符串
       sb.append("java no.1");
       System.out.println(sb.toString());
       sb.append("java xiang");
       System.out.println(sb.toString());
       sb.append("java no.2");
       System.out.println(sb.toString());
       
       //2.insert();在任意位置添加
       sb.insert(0, "我在最前面");
       System.out.println(sb.toString());
       
       //3.replace();Sting的replace只能替换旧的字符或子字符串,而这个替换可以指定位置。
       sb.replace(0, 4, "hello"); //替换的位置包含头部不包含尾部
       
       //4.delete();可以有选择的把字符串缓冲区内容删除一部分
       sb.delete(0, 5);
       System.out.println(sb.toString());
       
       //5.清空所有数据
       sb.delete(0, sb.length());
       System.out.println(sb.toString());
       System.out.println(sb.length());
  }
}

//验证StringBuffer的效率
package com.qf.demo;

public class demo6{
   public static void main(String[] args){
       //开始时间
       long start = System.currentTimeMillis();
       /*
       String str = "";
       for(int i=0; i<99999; i++){
           str+=i; //这里不是加法而是字符串的拼接
       }
       System.out.println(str);
       */
       StringBuilder sb = new StringBuilder();
       for(int i=0; i<99999; i++){
           sb.append(i);
      }
       System.out.println(sb.toString());
       
       //结束时间
       long end = System.currentTimeMills();
       System.out.println("耗时:" + (end - start));
  }
}

posted on   愿将过往均储藏  阅读(252)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示