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 | 29 | 30 |
Tags
- 반응형웹
- Database
- FileWriter
- FFT
- inline
- Position
- oracle
- java
- ObjectOutputStream
- 푸리에 변환
- BorderLayout
- 파이썬
- Codility
- css 기초
- 사전학습
- CSS
- GridLayout
- html 기초
- 상속
- 퍼셉트론
- iframe 태그
- 메서드
- HTML
- FlowLayout
- html 프로젝트
- 미디어쿼리
- g검정
- 반응형 웹 프로젝트
- 예제
- rnn
Archives
- Today
- Total
도라에몽주머니
[Java/Eclipse] Java 33일차 본문

특정 문자열의 위치 조회
- indexOf(찾으려는 문자, 시작할 위치)
- indexOf(String str)
- indexOf(int ch)
- indexOf(int ch, int fromIndex)
- indexOf(String str, int fromIndex)
: 특정 문자열이 처음으로 나타나는 위치를 조회함. 만약 해당 문자열을 찾지 못했을 때는 -1을 반환. 시작할 위치는 생략 가능
public class Ex {
public static void main(String[] args) {
String str1 = "자바 Programming";
String str2 = new String("Java");
System.out.println(str1.indexOf(" "));
System.out.println(str2.indexOf("a"));
System.out.println(str2.indexOf("a", 2));
}
}
- lastIndexOf()
- lastIndexOf(String str)
- lastIndexOf(int ch)
- lastIndexOf(int ch, int fromIndex)
- lastIndexOf(String str, int fromIndex)
: 뒤에서부터 확인했을 때, 특정 문자열이 처음으로 나타나는 위치를 조회함. 만약 해당 문자열을 찾지 못했을 때는 -1을 반환.
public class Ex {
public static void main(String[] args) {
String str1 = "자바 Programming";
String str2 = new String("Java");
System.out.println(str1.lastIndexOf(" "));
System.out.println(str1.lastIndexOf("ttt")); // return -1
System.out.println(str2.lastIndexOf("a"));
}
}
문자열의 앞,뒤 공백 문자 제거
- trim()
: 문자열의 앞, 뒤에 있는 공백을 제거함. 문자 중간에 있는 공백은 제거하지 않음.
public class Ex {
public static void main(String[] args) {
String str = " te st ";
System.out.println(str + "ttt");
String r = str.trim();
System.out.println(r + "ttt");
}
}
문자열 자르기
- substring(int index)
: index 부터 문자열의 끝까지 문자열을 잘라냄.
- substring(int start, int end)
: start부터 end 전까지 문자열을 잘라냄
* indexOf로 찾은 문자열의 위치를 substring에 활용해 문자열을 자르기도 함
public class Ex {
public static void main(String[] args) {
String str1 = "자바 Programming";
String r8 = str1.substring(3, 9);
String r82 = str1.substring(9);
System.out.println(r8);
System.out.println(r82);
}
}
데이터를 문자열로 변환
- String.valueOf()
: 특정 타입의 데이터를 문자열로 변환.
* toString()과 String.valueOf()의 차이 : https://swjeong.tistory.com/146
public class Ex {
public static void main(String[] args) {
int a = 5;
long b = 1234567890123456L;
float c = 3.14f;
double d = 123.4567;
boolean e = false;
// valueOf(데이터) => 데이터를 문자열로 변경함
String str1 = String.valueOf(a);
String str2 = String.valueOf(b);
String str3 = String.valueOf(c);
String str4 = String.valueOf(d);
String str5 = String.valueOf(e);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println(str4);
System.out.println(str5);
}
}
Calender 클래스
: 날짜와 시간에 관한 데이터를 쉽게 처리할 수 있도록 하는 추상 클래스
- 시간 추출
import java.util.Calendar;
public class Ex {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.toString());
// 년, 월, 일, 시, 분, 초 추출
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH) + 1; // month는 0부터 시작
int dd = calendar.get(Calendar.DAY_OF_MONTH);
int hh = calendar.get(Calendar.HOUR_OF_DAY);
int mi = calendar.get(Calendar.MINUTE);
int ss = calendar.get(Calendar.SECOND);
System.out.printf("%d년 %02d월 %02d일 %02d시 %02d분 %02d초\n", yy, mm, dd, hh, mi, ss);
System.out.println("------------------------------");
// 12시간제
hh = calendar.get(Calendar.HOUR);
int ampm = calendar.get(Calendar.AM_PM);
String[] apname = {"오전", "오후"};
System.out.printf("%d년 %02d월 %02d일 %s %02d시 %02d분 %02d초\n", yy, mm, dd, apname[ampm], hh, mi, ss);
System.out.println("------------------------------");
}
}
- 시간 설정
import java.util.Calendar;
public class Ex {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
DatePrinter.printDate(calendar);
// 날짜와 시간 수정 1
calendar.set(Calendar.YEAR, 2020);
calendar.set(Calendar.MONTH, 11);
calendar.set(Calendar.DAY_OF_MONTH, 25);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 12);
calendar.set(Calendar.SECOND, 12);
DatePrinter.printDate(calendar);
// 날짜와 시간 수정 2
calendar.set(2021, 4, 5); // 2021년 5월 5일
DatePrinter.printDate(calendar);
// 날짜와 시간 수정 3
calendar.set(2022, 6, 7, 7, 7, 7); // 2022년 7월 7일 7시 7분 7초
DatePrinter.printDate(calendar);
}
}
'Study > Java' 카테고리의 다른 글
| [Java/Eclipse] Java 36, 37일차 (0) | 2022.11.07 |
|---|---|
| [Java/Eclipse] Java 34, 35일차 (0) | 2022.11.03 |
| [Java/Eclipse] Java 32일차 (0) | 2022.10.31 |
| [Java/Eclipse] Java 31일차 (0) | 2022.10.28 |
| [Java/Eclipse] Java 30일차 (0) | 2022.10.27 |