Documentation

The Java™ Tutorials
Hide TOC
Comparing Strings and Portions of Strings比较字符串和字符串的部分
Trail: Learning the Java Language
Lesson: Numbers and Strings
Section: Strings

Comparing Strings and Portions of Strings比较字符串和字符串的部分

The String class has a number of methods for comparing strings and portions of strings.String类有许多方法用于比较字符串和字符串的一部分。The following table lists these methods.下表列出了这些方法。

Methods for Comparing Strings比较字符串的方法
Method方法 Description描述
boolean endsWith(String suffix)
boolean startsWith(String prefix)
Returns true if this string ends with or begins with the substring specified as an argument to the method.如果此字符串以指定为方法参数的子字符串结尾或开头,则返回true
boolean startsWith(String prefix, int offset) Considers the string beginning at the index offset, and returns true if it begins with the substring specified as an argument.考虑从索引offset开始的字符串,如果以指定为参数的子字符串开始,则返回true
int compareTo(String anotherString) Compares two strings lexicographically.按字典顺序比较两个字符串。Returns an integer indicating whether this string is greater than (result is > 0), equal to (result is = 0), or less than (result is < 0) the argument.返回一个整数,指示此字符串是否大于(结果为>0),等于(结果为=0)或小于(结果为<0)参数。
int compareToIgnoreCase(String str) Compares two strings lexicographically, ignoring differences in case.按字典顺序比较两个字符串,忽略大小写的差异。Returns an integer indicating whether this string is greater than (result is > 0), equal to (result is = 0), or less than (result is < 0) the argument.返回一个整数,指示此字符串是否大于(结果为>0),等于(结果为=0)或小于(结果为<0)参数。
boolean equals(Object anObject) Returns true if and only if the argument is a String object that represents the same sequence of characters as this object.当且仅当参数是表示与此对象相同的字符序列的String对象时,返回true
boolean equalsIgnoreCase(String anotherString) Returns true if and only if the argument is a String object that represents the same sequence of characters as this object, ignoring differences in case.当且仅当参数是表示与此对象相同的字符序列的String对象时返回true,忽略大小写差异。
boolean regionMatches(int toffset, String other, int ooffset, int len) Tests whether the specified region of this string matches the specified region of the String argument.测试此字符串的指定区域是否与字符串参数的指定区域匹配。

Region is of length len and begins at the index toffset for this string and ooffset for the other string.区域的长度为len,从该字符串的索引toffset和其他字符串的ooffset开始。

boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) Tests whether the specified region of this string matches the specified region of the String argument.测试此字符串的指定区域是否与字符串参数的指定区域匹配。

Region is of length len and begins at the index toffset for this string and ooffset for the other string.区域的长度为len,从该字符串的索引toffset和其他字符串的ooffset开始。

The boolean argument indicates whether case should be ignored; if true, case is ignored when comparing characters.布尔参数指示是否应忽略大小写;如果为true,则在比较字符时忽略大小写。

boolean matches(String regex) Tests whether this string matches the specified regular expression.测试此字符串是否与指定的正则表达式匹配。Regular expressions are discussed in the lesson titled "Regular Expressions."“正则表达式”课程中讨论了正则表达式

The following program, RegionMatchesDemo, uses the regionMatches method to search for a string within another string:以下程序RegionMatchesDemo使用regionMatches方法在另一个字符串中搜索字符串:

public class RegionMatchesDemo {
    public static void main(String[] args) {
        String searchMe = "Green Eggs and Ham";
        String findMe = "Eggs";
        int searchMeLength = searchMe.length();
        int findMeLength = findMe.length();
        boolean foundIt = false;
        for (int i = 0; 
             i <= (searchMeLength - findMeLength);
             i++) {
           if (searchMe.regionMatches(i, findMe, 0, findMeLength)) {
              foundIt = true;
              System.out.println(searchMe.substring(i, i + findMeLength));
              break;
           }
        }
        if (!foundIt)
            System.out.println("No match found.");
    }
}

The output from this program is Eggs.这个程序的输出是Eggs

The program steps through the string referred to by searchMe one character at a time.程序一步一个字符地遍历searchMe引用的字符串。For each character, the program calls the regionMatches method to determine whether the substring beginning with the current character matches the string the program is looking for.对于每个字符,程序调用regionMatches方法来确定以当前字符开头的子字符串是否与程序正在查找的字符串匹配。


Previous page: Manipulating Characters in a String
Next page: The StringBuilder Class