-
210819_split+tokenizerJAVA 2021. 8. 19. 15:14
//문자열을 특정문자로 분리하는 방법들
String str= "red,blue,green,white,yellow";
System.out.println("#1.string 객체의 split을 이용한 분리");
//배열처리해서 해야 함
String[]arr=str.split(","); //()안에 구분객체 ","를 넣어줌
System.out.println("총 "+arr.length+"개");
//문자 출력
for(int i=0;i<arr.length;i++)
{
System.out.println(i+1+": "+arr[i]);
}
System.out.println("#2.stringTokenizer 객체를 이용한 분리");
//stringTokenizer 객체 생성
StringTokenizer st=new StringTokenizer(str, ",");
System.out.println("총 토큰수: "+st.countTokens());
while(st.hasMoreTokens()) //다음 토큰 있으면 true 반환, 없으면 false
{
System.out.println(st.nextToken()); //다음 토큰값 얻기
}
//결과
#1.string 객체의 split을 이용한 분리
총 5개
1: red
2: blue
3: green
4: white
5: yellow
#2.stringTokenizer 객체를 이용한 분리
총 토큰수: 5
red
blue
green
white
yellow
'JAVA' 카테고리의 다른 글
210819_date format (0) 2021.08.19 210819_filereader+bufferreader+split+tokenizer (0) 2021.08.19 210819_Exception(리뷰) (0) 2021.08.19 210818_Exception (0) 2021.08.18 210818_익명내부클래스+상속 (0) 2021.08.18