Comparing Strings and Portions of Strings
클래스 String
에는 문자열과 문자열 일부를 비교하는 많은 메소드들이 들어 있습니다.
다음 표는 이들 메소드의 목록입니다.
The String
class has a number of methods for comparing strings and portions of strings. The following table lists these methods.
Method | Description |
---|---|
boolean endsWith(String suffix) | 문자열이 메소드의 아규먼트에 명시된 substring으로 끝나거나 시작될 경우 true를 리턴시킵니다. Returns |
boolean startsWith(String prefix, int offset) | 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 |
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 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.
'Java 배우기' 카테고리의 다른 글
Summary of Characters and Strings 글자와 문자열 요약 (0) | 2016.01.05 |
---|---|
The StringBuilder Class 클래스 StringBuilder (0) | 2016.01.05 |
Manipulating Characters in a String String 안에서 글자 조작하기 (0) | 2016.01.05 |
Converting Between Numbers and Strings 숫자와 문자 사이의 변환 (0) | 2016.01.05 |
Strings 문자열 (0) | 2016.01.05 |
댓글