수업

+25 라이브러리

hs_developer 2022. 6. 8. 17:52

 

substring

 

1. 글자 수 벗어난 경우 자르기

문자열 입력: 
dfadfafdfafdfafd
입력한 문자열: dfadfafdfafdfafd
글자 수: 16
dfadfafdfa...
public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("문자열 입력: ");
    String ss = sc.nextLine();

    System.out.println("입력한 문자열: " + ss);
    System.out.println("글자 수: " + ss.length());

    // 글자 수 벗어난 경우
    if(ss.length() > 10)
    {
        ss = ss.substring(0, 10) + "...";
    }
    System.out.println(ss);
}

 

 

2. 확장자명 불러오기

txt
gif
public static void main(String[] args) {
		
		Set<String> set = new HashSet<String>(); // 중복 제거
		try
		{
			File dir = new File("c:\\javaDev");
			File[] list = dir.listFiles(); // 폴더에 있는 모든 파일 불러오기
			
			// 폴더, 파일 불러오기
			for(File f:list)
			{
				if(f.isFile()) 
				{
					// 폴더 없이 파일만 불러오기
//					System.out.println(f.getName());
					
					// 파일 확장자 불러오기
					String name = f.getName();
					String ext = name.substring(name.lastIndexOf(".") + 1);
//					System.out.println(ext);
					
					set.add(ext); // 중복 제거
				}
			}
		}
		catch(Exception ex){}
		
		// 중복 제거
		for(String s:set)
		{
			System.out.println(s);
		}
	}

 

 

StringBuffer

-문자열 결합 (임시 저장 장소)

 

-append() : 문자열 결합

 

 

....
})(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');
</script-->


</body>
</html>
걸린 시간: 49
public static void main(String[] args) {
		
    try
    {
        StringBuffer data = new StringBuffer();
//			String data = "";
        long start = System.currentTimeMillis();
        FileReader fr = new FileReader("c:\\javaDev\\recipe.txt");

        int i=0;
        while((i=fr.read())!=-1)
        {
//				data += String.valueOf((char)i);
            data.append((char)i);
        }
        long end = System.currentTimeMillis();
        System.out.println(data);
        System.out.println("걸린 시간: " + (end-start));

    } catch(Exception ex){}

}

 

 

 

Math

페이지 나누기

70
7
public static void main(String[] args) {
		
    int no = (int)(Math.random()*100)+1;
    System.out.println(no);

    // 페이지 나누기
    int totalPage = (int)(Math.ceil(no/10.0));
    System.out.println(totalPage);
}