字符串的创建及常用方法

本文详细介绍了Java中字符串的创建方式、常用方法如获取长度、比较、截取、替换以及去除空格等,并通过实例演示了如何使用这些方法。
摘要由CSDN通过智能技术生成

目录

1.创建字符串

2.字符串的常用方法

1.public int length()

2.public boolean equals(String s)

3.public boolean startsWith(String s) 和public boolean endsWith(String s)

4.public boolean regionMatches(int firstStart,String other,int otherStart,int length)

实例

5.public int compareTo(String s)

6.public int indexOf(String s)

7.public String substring(int startpoint)

8.public String replaceAll(String oldString,String newString)

9.public String trim()


字符串常量:"你好""1234.978""weqweo"

申明字符串: String s;;

1.创建字符串

  1. 使用String类的构造方法。如:

    s=new String("Hello world");
  2. 使用已经创建好的字符串创建另一个字符串。如:

    String s = new String (s);
  3. 使用字符数组创建字符串对象,如:

    char a[] = {'s','t','r','i','n','g'};
    String s = new String(a);//该语句与s=new String("string")等价
  4. 提取字符数组中的一部分创建一个字符串对象,如:

    char a[] = {'s','t','r','i','n','g'};
    String s = new String(a,2,4);//该语句与s=new String("ring")等价

引用字符串常量对象

字符串常量是对象,因此可以把字符串常量的引用赋值给另一个字符串变量。如:

String s1,s2;
s1="hello";
s2="hello";//s1,s2具有了相同的引用

2.字符串的常用方法

1.public int length()

获取字符串的长度。如:

String s = "Hello world";
int n;
n=s.length();//n=11

字符串常量也可以使用该方法,如:

int n;
n="Hello world".length();//n=11
2.public boolean equals(String s)

字符串比较,比较当前字符串是否与s相同。如:

String s1="Hello world";
String s2="hello world";
String s3="Hello world";
boolean r1 = s3.equals(s1);//true
boolean r2 = s3.equals(s2);//false
  • s1==s2的指为false。因为字符串是对象 ,s1、s2是引用,s1与s2所指向的并非是同一个字符串。

调用public boolean equalsIgnoreCase(String s) 也是比较两个字符串是否相同,但忽略大小写

3.public boolean startsWith(String s)public boolean endsWith(String s)
  • 字符串对象调用startsWith(String s)方法,判断当前字符串对象的前缀(包括当前字符串本身)是否含有参数指定的字符串s。

  • 字符串对象调用startsWith(String s)方法,判断当前字符串对象的后缀(包括当前字符串本身)是否含有参数指定的字符串s。

String s = "Hello world";
boolean boo = s.startsWith("He");// boo = true
boolean boo1 = s.endsWith("wo");//boo1 = false
4.public boolean regionMatches(int firstStart,String other,int otherStart,int length)

从当前字符串参数 firstStant 指定的位置开始处,取长度为 length 的一个子串,并将这个子串和参数 other 指定的一个子串进行比较。其中,other 指定的子串是从参数 othertStart 指定的位置开始,从other中取长度为length 的一个子串。如果两个子串相同(未忽略大小写)该方法就返回true,否则返回 false。如:

String s = "Hello world";
String s1 = "Hello";
String s = "Hello world";
boolean boo = s.regionMatches(0,s1,0,5);
System.out.println(boo); //true
  • 第4行的意思为:从字符串s下标为0的字符开始,取长度为5的字符串,即Hello;从字符串s1下标为0的字符开始,取长度为5的字符串,也是Hello比较取出的两个子串是否相同;

  • 若第4行改为boolean boo = s.regionMatches(0,s1,1,3);则返回false,因为从字符串s下标为0的字符开始,取长度为3的字符串,即Hel;从字符串s1下标为1的字符开始,取长度为3的字符串,是ell,取出的两个子串不相同;

  • 该方法的重载方法是:

    public boolean regionMatches(boolean b,int firstStart,String other,int otherStart,int length)

可以通过参数b决定是否忽略大小写,当b=true时忽略大小写。

实例

判断一个字符串 student;entropy;engage;english;client 中共出现几个en;

public class Main {
    public static void main(String[] args) {
        String s = "student;entropy;engage;english;client";
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.regionMatches(i, "en", 0, 2))
                count++;
        }
        System.out.println(count); //5
        return;
    }
}
5.public int compareTo(String s)

字符串对象可以使用 String类中的compareTo(Strings)方法,按字典序与参数s指定的字符串比较大小。如果当前字符串与s相同,该方法返回值0:如果当前字符串对象大于s,该方法返回正值;如果小于s,该方法返回负值。如:

String str = "abcde";
int a = str.compareTo("boy");//小于0
int b = str.compareTo("aba");//大于0
int c = str.compareTo("abcde");//等于0。
  • 按字典序比较两个字符串还可以使用publicintcompareTolgnoreCase(Strings)方法,该方法忽略大小写

6.public int indexOf(String s)

字符串调用 indexOf(Strings)方法从当前字符串的头开始检索字符串s,并返回首次出现s的位置。如果没有检索到字符串s,该方法返回的值是-1。如:

  • indexOf(Strings,int startpoint)方法

字符串调用indexOf(Strings,int startpoint)方法从当前字符串的 startpoint位置处开始检索字符串 s,并返回首次出现s的位置。如果没有检索到字符串s,该方法返回的值是-1。字符串调用lastlndexOf(Strings)方法从当前字符串的头开始检索字符串,并返回最后出现s的位置。如果没有检索到字符串s,该方法返回的值是-1

String s = "I am a good cat";
int n1=s.indexOf("a");//n1=2
n1=s.indexOf("good",2);//n1=7
n1=s.indexOf("a",7);//n1=13
n1=s.indexOf("w",2);//n1=-1
7.public String substring(int startpoint)

字符串对象调用该方法获得一个当前字符串的子串,该子串是从当前字符串的startpoint 处截取到字符串的末尾处所得到的字符串。

  • substring(int start ,intend)

字符串对象调用 substring(int start ,intend)方法获得一个当前字符串的子串,该子串是从当前字符串的 stant 处截取到 end 处所得到的字符串,但不包括 end 处所对应的字符

例如:

String s = "I love tom";
String s1 = s.substring(2,5);//s1="lov"
8.public String replaceAll(String oldString,String newString)

字符串对象s调用该方法可以返回一个串对象,这个串对象是通过用参数 newString 指定的字符串替换s中由oldString指定的所有字符串而得到的字符串。如果s中不包含串oldString则返回原串s。如:

String s="Hello He world";
String s1 = s.replaceAll("He","he"); //s1 = "hello he world"
9.public String trim()

一个字符串s通过调用trim()方法返回一个字符串对象,该字符串对象是s去掉前后空格后的字符串。

String s="   Hello He world   ";
String s1 = s.trim();
System.out.println(s1);//s1="Hello He world"
  • 22
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

__羽辞__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值