String类常用方法(Java)

本文介绍了如何使用JavaString类进行字符串替换、连接、比较(equals(),equalsIgnoreCase(),compareTo())以及查找子字符串和字符位置的方法,如replace(),concat(),contains(),substring(),和indexOf(),并展示了实际应用示例。
摘要由CSDN通过智能技术生成

String类

  1. 字符串相减/替换

使用 String 类的 replace() 方法来实现这个功能: replace(“target”,“replacement”)

如果你想将引号添加到字符串中,你可以使用转义字符 \" 来表示双引号。

String str1 = "abcdefg";
String str2 = "bcd";
String result = str1.replace(str2, "");
System.out.println(result);  // 输出: aefg
  1. 字符串连接

使用 String 类的 concat() 方法来实现这个功能: contact(String s)

也可以用StringBuilder类来添加,空间效率更快

 String s = "菜鸟教程";
 s = s.concat("网址:").concat("www.runoob.com");
 System.out.println(s);    //输出 菜鸟教程网址:www.runoob.com
  1. 比较子/字符串是否相同
  • equals() 方法
String str1 = "abc";
String str2 = "123a";
str1.equals(str2);    //输出 false
"abc".equals(str2);    //输出 true
  • equalsIgnoreCase() 方法 --不区分大小写
String str1 = "abc";
String str2 = "ABC";
System.out.println(str1.equalsIgnoreCase(str2));    // 输出 true
  • compareTo() 方法

compareTo() 方法用于按字典顺序比较两个字符串的大小,该比较是基于字符串各个字符的 Unicode 值。

String str1 = "A";
String str2 = "a";
System.out.println(str1.compareTo(str2));    // a - A 输出 32
System.out.println(str2.compareTo(str1));    // A - a 输出 -32
System.out.println(str2.compareTo("a"));    // 相同输出 0
  1. 字符串是否包含

contains() 方法

String str = "abcdef";
boolean statue = srt.contains("bc");    // statue = true;
  1. 获取子字符串
  • substring(int beginIndex)

    1. 这个版本的substring方法从指定的索引位置开始,提取字符串的一部分,一直到字符串的末尾。
  • substring(int beginIndex, int endIndex)

    • 这个版本的substring方法从指定的起始索引位置开始,提取字符串的一部分,直到结束索引之前的位置。

    • public class SubstringExample {
          public static void main(String[] args) {
              String s = "Hello, world!";
              // 第1个substring方法
              // 使用单个索引,提取从索引2开始到末尾的子字符串
              String substring1 = s.substring(2);
              System.out.println("Substring 1: " + substring1); // 输出:llo, world!
              
              // 第2个substring方法
              // 使用两个索引,提取从索引7到索引12之前的子字符串,即 7 - 11 
              String substring2 = s.substring(7, 12);
              System.out.println("Substring 2: " + substring2); // 输出:world
              
              // 提取索引4之前的子字符串(索引4不包括在内)
              String substring3 = s.substring(0, 4);
              System.out.println("Substring 3: " + substring3); // 输出:Hell
          }
      }
      
  1. 查找 字符/串 位置
  • int indexOf(String str)

    1. 这个版本的 indexOf 方法用于查找指定子字符串在字符串中第一次出现的索引位置。
  • int indexOf(String str,int fromIndex):

    • 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

    • public class IndexOfExample {
          public static void main(String[] args) {
              String text = "Hello, world! This is a sample text.";
              // 第1个indexOf方法
              // 查找字符 ',' 第一次出现的索引
              int commaIndex = text.indexOf(',');
              System.out.println( commaIndex); // 输出: 5
      
              // 查找子字符串 "world" 第一次出现的索引
              int worldIndex = text.indexOf("world");
              System.out.println( worldIndex); // 输出: 7
      
              // 查找不存在的字符或子字符串
              int notFoundIndex = text.indexOf("123");
              System.out.println( notFoundIndex); // 输出:-1
              
              // 第二个indexOf方法
              String t = text.indexOf("o",6);
              System.out.println( t); // 输出: 8
          }
      }
      
  • 23
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值