본문 바로가기
Java 배우기

Comparing Strings and Portions of Strings 문자열과 문자열 일부의 비교

by 노화방지 Anti-aging Hairstyle 2016. 1. 5.
반응형


클래스 String에는 문자열문자열 일부비교하는 많은 메소드들이 들어 있습니다.
다음 표는 이들 메소드의 목록입니다
.

The String class has a number of methods for comparing strings and portions of strings. The following table lists these methods.


문자열 비교를 위한 메소드 Methods for Comparing Strings

MethodDescription
boolean endsWith(String suffix)
boolean startsWith(String prefix)

문자열이 메소드의 아규먼트에 명시된 substring으로 끝나거나 시작될 경우 true를 리턴시킵니다.

Returns true if this string ends with or begins with the substring specified as an argument to the method.

boolean startsWith(String prefix, int offset)

index offset에서 시작되는 문자열을 고려하고, 아규먼트에 명시된 substring으로 시작될 경우 true를 리턴시킵니다.

Considers the string beginning at the index offset, and returns true if it begins with the substring specified as an argument.
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.
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.
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.
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.
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.

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.

The boolean argument indicates whether case should be ignored; if true, case is ignored when comparing characters.

boolean matches(String regex)Tests whether this string matches the specified regular expression. Regular expressions are discussed in the lesson titled "Regular Expressions."


다음 프로그램 RegionMatchesDemo는 다른 문자열 안의 문자열을 찾기 위하여 메소드 regionMatches를 사용합니다:
The following program, RegionMatchesDemo, uses the regionMatches method to search for a string within another string:

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.

The program steps through the string referred to by searchMe one character at a time. 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.


반응형

댓글