String 类的常用方法

  1. public int length() : 获取一个字符的长度.
String s = "Hello World!";
System.out.print(s.length());   //输出12
  1. public boolean equals(String s) : 比较当前字符串对象的实体是否与参数s指定的字符串的实体相同.
String s1 = "Hello World!";
String s2 = "Hello World!";
System.out.println(s1.equals(s2));   //比较s1,s2的实体是否相同,输出:true
System.out.println(s1 == s2);   //比较s1,s2的引用(地址)是否相同,输出:false
  1. ① public boolean startsWith(String s) : 判断当前字符串对象是否以字符串参数s指定的字符串开头.
String s = "Hello World!";
System.out.println(s.startsWith("He"))   //判断s是否以"He"开头,输出:true
System.out.println(s.startsWith("ld!"))   //判断s是否以"ld!"开头,输出:false

② public boolean endsWith(String s) : 判断当前字符串对象是否以字符串参数s指定的字符串结尾.

String s = "Hello World!";
System.out.println(s.endsWith("He"))   //判断s是否以"He"结尾,输出:false
System.out.println(s.endsWith("ld!"))   //判断s是否以"ld!"结尾,输出:true
  1. public boolean regionMatches(int firstStart,String other,int otherStart,int length) : 从当前字符串参数firstStart指定的位置开始,取长度为length的一个子串,并将这个子串与参数other从otherStart开始取形成的子串,二者进行比较.
    注 : 其相关方法 : public boolean regionMatches(boolean b,int firstStart,String other,int otherStart,int length)
    (其中的参数 b决定是否区分大小写,若为true,可以省略).
String s = "Hello World!";
System.out.println(s.regionMatches(0,"He",0,2));   /*将子串"He"与参数的子串"He"(此处的
otherStart为0,即other从头开始取,就是整个参数),输出:true*/
System.out.println(s.regionMatches(0,"SHe",1,2));   /*将子串"He"与参数的子串"He"(此处的
otherStart为1,就是"He"),输出:true*/
System.out.println(s.regionMatches(1,"He",0,2));   /*将子串"el"与参数的子串"He"(此处的
otherStart为0,即other从头开始取,就是整个参数),输出:false*/
System.out.println(s.regionMatches(0,"He",0,3));   /*将子串"Hel"与参数的子串"He"(此处的
otherStart为0,即other从头开始取,就是整个参数),输出:false*/
  1. public int compareTo(String s) :当前字符与参数s按字典序进行比较大小(返回值为第一位Unicode编码不同的差值).
String s = "Hello";
System.out.println(s.compareTo("World!"));   /*'H'的Unicode编码为72,'W'的Unicode编码为87,
二者的差为-15,则输出:-15*/
System.out.println(s.compareTo("ABCDE"));   /*'H'的Unicode编码为72,'A'的Unicode编码为65,
二者的差为7,则输出:7*/
  1. public boolean contains(String s ) : 判断当前字符串对象是否含有参数指定的字符串s.
String s = "Hello World!";
System.out.println(s.contains("World!"));   //输出:true
System.out.println(s.contains("Java!"));   //输出:false
  1. public int indexOf(String s) : 从当前字符串的头开始检索字符串s,并返回首次出现s的位置,找不到则返回-1.
String s = "Hello World!";
System.out.println(s.indexOf("World!"));   //输出:6
System.out.println(s.indexOf("Java!"));   //输出:-1

其他相关方法:
① public int indexOf(String s,int startpiont) : 从当前字符串的startpiont位置开始,检索字符串s,并返回首次出现s的位置,找不到则返回-1.
② public int lastIndexOf(String s) : 从当前字符串的后面开始,检索字符串s,并返回首次出现s的位置(正序位置),找不到则返回-1.

  1. public String substring(int startpiont) : 获得一个当前字符串的子串(从startpiont位置开始).
String s = "Hello World!";
System.out.println(s.substring(6));   //输出:"World!"

其他相关方法 : public String substring(int start,int end) : 获取一个当前字符串的子串(从start位置开始,到end位置结束).

  1. public String trim() : 得到一个s去掉前后空格后的子串.
String s = " Hello World! ";
System.out.println(s.trim());   //输出:"Hello World!"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值