"bs".endsWith()的使用注意,别用错了

"bs"

bs.endsWith(s1)和s1.endsWith("bs"),差别很大

描述

java.lang.String.endsWith() 方法返回的测试该字符串是否以指定后缀sffix结束

声明

以下是声明java.lang.String.endsWith()方法

public boolean endsWith(String suffix)

参数

  • suffix -- This is the suffix.

返回值

该方法返回一个true,如果参数表示的字符序列是由该对象表示的字符序列的后缀,否则返回false.

异常

  • NA

实例

下面的示例演示使用的java.lang.String.endsWith()方法.

package com.yiibai;

import java.lang.*;

public class StringDemo {

  public static void main(String[] args) {
  
    String str = "www.yiibai.com";
    System.out.println(str);

    // the end string to be checked
    String endstr1 = ".com";
    String endstr2 = ".org";

    // checks that string str ends with given substring
    boolean retval1 = str.endsWith(endstr1);
    boolean retval2 = str.endsWith(endstr2);

    // prints true if the string ends with given substring
    System.out.println("ends with " + endstr1 + " ? " + retval1);
    System.out.println("ends with " + endstr2 + " ? " + retval2);
  }
}

让我们来编译和运行上面的程序,这将产生以下结果:

www.yiibai.com
ends with .com ? true
ends with .org ? false
posted on 2013-02-04 21:45  沽月  阅读(407)  评论(0编辑  收藏  举报