이 기사에서는 문자열이 텍스트에 나타나는 횟수 또는 지정된 요소가 문자열에 나타나는 횟수를 계산하는 방법을 설명합니다. 참조를 위해 귀하와 공유됩니다. 특정 내용은 다음과 같습니다
실행 렌더링 :
이 파일에서 프로그램은 "A"가있는 문자를 몇 번이나 찾습니까?
특정 코드는 다음과 같습니다
패키지 com.zuidaima.util.string; java.io.*; 공개 클래스 카운트 스트링 {public static int count (문자열 파일 이름, 문자열 target)는 filenotfoundException, ioException {filereader fr = new Filereader (filename); bufferedReader br = 새로운 버퍼드 리더 (FR); StringBuilder strb = new StringBuilder (); while (true) {string line = br.readline (); if (line == null) {break; } strb.append (line); } 문자열 결과 = strb.tostring (); int count = 0; int index = 0; while (true) {index = result.indexof (target, index + 1); if (index> 0) {count ++; } else {break; }} br.close (); 반환 수; } public static void main (string [] args) {try {system.out.println ( "count ("d : //zuidaima.txt ","a ")); } catch (filenotfoundException e) {e.printstacktrace (); } catch (ioexception e) {e.printstacktrace (); }}}위의 것은 Java 문자열에서 요소 발생 수를 명시하는 방법입니다. 모든 사람의 학습에 도움이되기를 바랍니다.