java string 특정 문자 갯수 구하기
출처 :
package test2;
import java.util.regex.Matcher; import java.util.regex.Pattern; /* 요구사항 : db에 저장된 설명 중 2번째 줄까지 내용만 보여 달라.
의 갯수를 세서 3개 일 때 까지의 길이를 구한 다음. substring으로 해당 문자 까지 잘라줌. / public class TestDate { public static void main(String[] args) { String s = ”
dddd
ddgggg
bbbbbttt
”; Pattern p = Pattern.compile(“
”);
Matcher m = p.matcher(s); int count = 0; int count2 = 0; for( int i = 0; m.find(i); i = m.end()){ if(count<4){ count++; count2=i; } } System.out.println(count+” ” + count2); //특정문자열(Pattern)의 갯수 System.out.println(s.substring(0,count2)); //특정 문자 갯수까지 출력하기 } }
This article is licensed under CC BY 4.0 by the author.