자바 – 문자열 찾기 关于本站ContactSearch

contains

public boolean contains(CharSequence s)
Returns true if and only if this string contains the specified sequence of char values.
Parameter:
s - the sequence to search for
Returns:
true if this string contains s, false otherwise
Since:
1.5

indexOf

public int indexOf(int ch)
Returns the index within this string of the first occurrence of the specified character. If a character with value ch occurs in the character sequence represented by this String object, then the index (in Unicode code units) of the first such occurrence is returned. For values of ch in the range from 0 to 0xFFFF (inclusive), this is the smallest value k such that:
this.charAt(k) == ch

is true. For other values of ch, it is the smallest value k such that:
this.codePointAt(k) == ch

is true. In either case, if no such character occurs in this string, then -1 is returned.
Parameters:
ch - a character (Unicode code point).
Returns:
the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

matches

public boolean matches(String regex)
Tells whether or not this string matches the given regular expression.
An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression

Pattern.matches(regex, str)
Parameters:
regex - the regular expression to which this string is to be matched
Returns:
true if, and only if, this string matches the given regular expression
Throws:
PatternSyntaxException - if the regular expression’s syntax is invalid
Since:
1.4
See Also:
Pattern

백문이 불여일타

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

* Created by ceedlive on 2017. 9. 2..
*/
public class {

public static void main(String[] args) {

//variables
String description = "배송지 정보: 인천광역시 서구, "
+ "주문도서: 진짜 잘 이해되는 중학 영문법 1"
+ "정가: (14,000원)"
+ "할인가: (12,600원)"
;

String mobileNo = "010-1234-5678";
String homeNo = "032-123-4567";
String email = "[email protected]";

String regexMobileNo = "^01(?:0|1|[6-9])-(?:\d{3}|\d{4})-\d{4}$";//휴대폰 번호 검증
String regexHomeNo = "^(02|0[3-9]{1}[0-9]{1})-(?:\d{4}|\d{3})-\d{4}$";//집/사무실 전화번호 검증
String regexEmail = "^[_a-z0-9-]+(.[_a-z0-9-]+)*@(?:\w+\.)+\w+$";

//try

//contains
if (description.contains("인천광역시 서구")) {
System.out.println(description.contains("인천광역시 서구"));//true
System.out.println("배송비 무료");
} else {
System.out.println("배송비 유료");
}

//indexOf
if (description.indexOf("인천광역시 서구") > -1) {
System.out.println(description.indexOf("인천광역시 서구"));//8
System.out.println("배송비 무료");
} else {
System.out.println("배송비 유료");
}

//matches
if (description.matches(".*인천광역시 서구.*")) {
System.out.println(description.matches(".*인천광역시 서구.*"));//true
System.out.println("배송비 무료");
} else {
System.out.println("배송비 유료");
}

//matches
if (description.matches(".*[0-9].*")) {
System.out.println(description.matches(".*[0-9].*"));//true
System.out.println("숫자 있음");
} else {
System.out.println("숫자 없음");
}

if (mobileNo.matches(regexMobileNo)) {
System.out.println("휴대폰 번호 형식과 일치");
} else {
System.out.println("휴대폰 번호 형식과 일치하지 않음");
}

if (homeNo.matches(regexHomeNo)) {
System.out.println("집/사무실 번호 형식과 일치");
} else {
System.out.println("집/사무실 번호 형식과 일치하지 않음");
}

if (email.matches(regexEmail)) {
System.out.println("이메일 형식과 일치");
} else {
System.out.println("이메일 형식과 일치하지 않음");
}

}
}

참고

Class String https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#method.summary