Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- control
- Pipelining
- for
- XML
- php
- Algorithm
- function
- python
- DB
- Linux
- web
- MIPS
- while
- MacOS
- DoM
- DS
- architecture
- DATAPATH
- computer
- react
- javascript
- Java
- Class
- system
- CSS
- html
- mysql
- instruction
- github
- data structure
Archives
- Today
- Total
YYYEJI
[JAVA] String 클래스 매소드 정리 본문
728x90
String(문자열)은
순차적으로 나열된 문자들입니다.
• Primitive data type X
• Class type O
자바에서 String은 primitive type이 아닌 class type입니다.
Class type이기 때문에
String class가 가지고 있는 method를 사용할 수 있습니다.
String constant를 변수에 담기 위해서는 따옴표를 사용합니다.
char one_character = 'a'; // Single quotes
String sentence = "Hello, wolrd"; // Double quotes
System.out.println(one_character);
System.out.println(sentence);
✓ 문자 하나를 담을 때는 외따옴표('')를 사용
✓ 문자열을 담을 때는 쌍따옴표("")를 사용
문자열에 문자(or 문자열)을 담고 싶을 땐 "+"(concatenating)를 사용합니다.
public static void main(String[] args) {
String greeting = "Hi, there! ";
greeting = greeting + "Welcome to my blog";
System.out.println(greeting);
}
String class METHOD 정리
① charAt(number) - 특정 인덱스의 문자 반환
public static void main(String[] args) {
String greeting = "Welcome to My Blog";
char c = greeting.charAt(14);
System.out.println(c);
}
② indexOf(String s) - 특정 문자의 인덱스 반환
public static void main(String[] args) {
String greeting = "Welcome to My Blog";
int i = greeting.indexOf("B");
System.out.println(i);
}
③ indexOf(String s, formIndex) - 인덱스 formindex부터 시작해서 특정 문자의 인덱스 반환
public static void main(String[] args) {
String greeting = "Welcome to My Blog";
int i = greeting.indexOf("o", 8);
System.out.println(i);
}
✓ 인덱스 5번째에 o가 있지만 인덱스 8번부터 찾기 때문에 9가 반환됩니다.
✓ 찾기 못하면 "-1" 반환합니다.
④ lastIndexOf(String s) - 인덱스의 마지막부터 시작해서 특정 문자의 인덱스 반환
public static void main(String[] args) {
String greeting = "Welcome to My Blog";
int i = greeting.lastIndexOf('c');
System.out.println(i);
}
⑤ substring(int index) - index부터 마지막 index까지 문자열 반환
public static void main(String[] args) {
String greeting = "Welcome to My Blog";
String s = greeting.substring(11);
System.out.println(s);
}
⑥ substring(int begin_index, int end_index) - 시작 문자열부터 끝 문자열까지 문자열 반환
public static void main(String[] args) {
String greeting = "Welcome to My Blog";
String s = greeting.substring(14, 18);
System.out.println(s);
}
⑦ replace(String s1, String s2) - s1의 문자열을 검색해서 s2로 변경해서 문자열 반환
public static void main(String[] args) {
String greeting = "Welcome to My Blog";
String s = greeting.replace("My", "YYYEJI's");
System.out.println(s);
}
⑧ concat(String s) - 원래 존재하던 문자열에 s 문자열 합친 결과 반환
public static void main(String[] args) {
String greeting = "Welcome to ";
String s = greeting.concat("YYYEJI's Blog");
System.out.println(s);
}
⑨ isEmpty() - 빈 문자열인지 확인하고 True/False 반환
public static void main(String[] args) {
String greeting = "";
boolean s = greeting.isEmpty();
System.out.println(s);
}
⑩ split(String s) - s를 기준으로 문자열을 자른 결과 담긴 array 반환
public static void main(String[] args) {
String greeting = "Welcome to My Blog";
String[] s = greeting.split(" ");
for(int i = 0; i<s.length; i++){
System.out.println(s[i]);
}
}
⑪ contains(String s) - s 문자열이 포함되어 있으면 True 반환
public static void main(String[] args) {
String greeting = "Welcome to My Blog";
boolean s = greeting.contains("Blog");
System.out.println(s);
}
⑫ startsWith(String s) - s문자열로 시작하면 True 반환
public static void main(String[] args) {
String greeting = "Welcome to My Blog";
boolean s = greeting.startsWith("Welcome");
System.out.println(s);
}
⑬ startsWith(String s, int index) - index 번째가 s문자열로 시작하면 True 반환
public static void main(String[] args) {
String greeting = "Welcome to My Blog";
boolean s = greeting.startsWith("My", 11);
System.out.println(s);
}
⑭ endsWith(String s) - 문자열 s로 끝나면 True 반환
public static void main(String[] args) {
String greeting = "Welcome to My Blog";
boolean s = greeting.endsWith("Blog");
System.out.println(s);
}
⑮ toUpperCase() - 대문자로 변환해서 문자열 반환
public static void main(String[] args) {
String greeting = "Welcome to My Blog";
String s = greeting.toUpperCase();
System.out.println(s);
}
⑯ lowerUpperCase() - 대문자로 변환해서 문자열 반환
public static void main(String[] args) {
String greeting = "Welcome to My Blog";
String s = greeting.toLowerCase();
System.out.println(s);
}
⑰ trim() - 시작과 끝의 공백 제거해서 문자열 반환
public static void main(String[] args) {
String greeting = " Welcome to My Blog ";
String s = greeting.trim();
System.out.println(s);
}
⑱ length() - 문자열의 길이 반환
public static void main(String[] args) {
String greeting = "Welcome to My Blog";
int s = greeting.length();
System.out.println(s);
}
◡̈
'Java' 카테고리의 다른 글
[JAVA] 자바 console에 결과 출력하기 (0) | 2022.10.23 |
---|---|
[JAVA] Truncates 란? (0) | 2022.10.23 |
[JAVA] 형변환(Casting) 이란? (0) | 2022.10.22 |
[JAVA] Primitive data type의 종류 (0) | 2022.10.22 |
[JAVA] 예약어와 키워드의 차이점 (Keyword, Reserved word) (0) | 2022.10.22 |