字符串的比较

1 、总体来说java中字符串的比较是==比较引用equals 比较值的做法
(equals 对于其他引用类型比较的是地址,这是因为object的equals方法比较的是引用),但是不同的声明方法字符串的比较结果也是不同的。
string s1="Hello"; 
string s2="Hello"; 
则(s1==s2)=true; 
因为他们指向的同一个对象。就是对同一个对象的引用
 
如果:
String s1=new String("Hello"); 
String s2=new String("Hello"); 
则(s1==s2)=false   因为开辟了不同的地址,是两个不同的对象

如果把其他变量的值赋给s1和s2,即使内容相同,由于不是指向同一个对象,也会返回false。所以建议使用equals(),因为equals比较的才是真正的内容 

例如: 
String string1=new String( "aaa" ); 
String string2=new String( "aaa" ); 
这两个字符串当然应该是相等的。 
如果用表达式string1==string2,则该表达式的值为false 
如果用表达式string1.equals(string2),则该表达式的值为true 
因此应该用string1.equals(string2),在if语句中就是 
if(string1.equals(string2)==true) //字符串相等,……

string1==string2,是值相等,而且内存地址也相等,是完全的相等 
string1.equals(string2)为true,只是值相等

2、regionMatches函数

测试两个字符串区域是否相等。

语法1 

public boolean regionMatches(boolean ignoreCase, int toffset, String other, int   ooffset, int len)

返回值:如果此字符串的指定子区域完全匹配字符串参数的指定子区域,则返回true;否则返回 false。

  参   数
 boolean ignoreCase 如果为 true,则比较字符时忽略大小写
 int toffset 此字符串中子区域的起始偏移量
 String other 字符串参数
 int ooffset 字符串参数中子区域的起始偏移量
 int len 要比较的字符数

本示例使用regionMatches方法,忽略大小写比较字符串strCom、subStr中从索引2开始的3个字符是否相等,返回布尔值。

String strCom = "ABLIKE";          //定义一个字符串

String subStr = "CDlike";          //定义一个字符串

boolean b = subStr.regionMatches(true, 2, strCom, 2, 3);  //比较两个字符串

System.out.println(b);

语法2  public boolean regionMatches(int toffset, String other, int ooffset, int len)

返回值:如果此字符串的指定子区域完全匹配字符串参数的指定子区域,则返回true;否则返回 false。

  参    数
 int toffset 此字符串中子区域的起始偏移量
 String other 字符串参数
 int ooffset 字符串参数中子区域的起始偏移量
 int len 要比较的字符数

本示例使用regionMatches方法,比较字符串strCom、subStr中从索引2开始的3个字符是否相等,返回布尔值。

String strCom = "ABLIKE";       //定义一个字符串

String subStr = "CDLIKE";        //定义一个字符串

boolean b = subStr.regionMatches(2, strCom, 2, 3); //比较两个字符串System.out.println(b);

import java.lang.*;

public class StringDemo {

  public static void main(String[] args) {
  
    String str1 = "Collection of tutorials";
    String str2 = "Consists of different tutorials";

    /* matches characters from index 14 in str1 to characters from
    index 22 in str2 considering same case of the letters */
    boolean match1 = str1.regionMatches(14, str2, 22, 9);
    System.out.println("region matched = " + match1);
    
    /* considering different case, "true" is set which will ignore
    case when matched */
    str2 = "Consists of different Tutorials";
    match1 = str1.regionMatches(true, 14, str2, 22, 9); 
    System.out.println("region matched = " + match1);   
  }
}

3、 startWith()和endWith()函数

  startWith()函数方法判断给定一个字符串(Stirng)是否从一个指定的字符串开始的。

  endWith()函数主要是指判断字符串是否是以一个指定的字符串结尾的。

 

     String st1 = "dsadas";
      System.out.println("st1:"+st1.startsWith("sa",1));
      String st2 = "dsadas";
      System.out.println("st1:"+st2.startsWith("sa"));
      String st3 = "dsadas";
      System.out.println("st2:"+st3.startsWith("ds"));
      String st4 = "dsadas";
      System.out.println("st4:"+st4.endWith("ds"));
      String st5 = "dsadas";
      System.out.println("st5:"+st5.endWith("as"));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值