예전에 지식인에 달았던 답변인데 일부 수정..
0. 공통
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class sep{
static String resSp(int num, String star){if(num > 0){
num--;
star += resSp(num, star);
}
return star;
}
}
1_1. CLASS
public class t0418_patternFind_1551 {
public static void main(String args[]){
Scanner scIn = new Scanner(System.in);
System.out.print("문자열에서 숨길문자의 자릿수를 입력 : ");
int num = scIn.nextInt();
String a = "1vcs24t";
Pattern pa = Pattern.compile(".{" + (num) +"}$");
Matcher rep = pa.matcher(a);
String mun = "";
while(rep.find()){
mun = a.replaceAll(rep.group(), sep.resSp(num - 1, "*"));
}
System.out.println(mun);
}
}
1_2. RUN
1v*****
2_1. CLASS
public class t0418_patternFind_1734 {
public static void main(String args[]){
Scanner scIn = new Scanner(System.in);
String a = " 1vcs24t abkcde1a 45ert43frdf";
int i = 0;
System.out.print("문자열에서 숨길문자의 자릿수를 입력 : ");
int num = scIn.nextInt();
Pattern pSep = Pattern.compile(".[^\\s\r\n|\n|\r]+");
Pattern pRep = Pattern.compile(".{" + num + "}$");
Matcher rep = pSep.matcher(a);
Matcher mRep = null;
System.out.println("\n---------------------------------------------------------------------------------------------------");
while(rep.find()){
if(rep.group() != null){
mRep = pRep.matcher(rep.group());
if(mRep.find()){
int no = rep.group().length() - 1 >= num ? num - 1 : (rep.group().length() - 1) - 1;
System.out.printf("%d. rep.group() : %s \t|| mRep.group() : %s", i, rep.group(), mRep.group());
System.out.println((rep.group().length() - 1) >= num ? "" : " - 입력한 자리수가 불러온 문자열의 자릿수 크기 보다 큽니다.");
a = a.replaceAll(mRep.group(), sep.resSp(no, "*"));
}
}
i++;
System.out.println("---------------------------------------------------------------------------------------------------");
}
System.out.println("\n" + a);
}
}
2_2. RUN
문자열에서 숨길문자의 자릿수를 입력 : 8
---------------------------------------------------------------------------------------------------
0. rep.group() : 1vcs24t || mRep.group() : 1vcs24t - 입력한 자리수가 불러온 문자열의 자릿수 크기 보다 큽니다.
---------------------------------------------------------------------------------------------------
1. rep.group() : abkcde1a || mRep.group() : abkcde1a
---------------------------------------------------------------------------------------------------
2. rep.group() : 45ert43frdf || mRep.group() : rt43frdf
---------------------------------------------------------------------------------------------------
******* ******** 45e********
한방에 처리하고 싶은데.. 루프 한번 돌때 처리하는 거니 상관없을려나?
좀 더 연구를 해봐야 할 듯..
'course > 자바' 카테고리의 다른 글
VO에 MAP등의 값을 대칭입력. (0) | 2017.09.27 |
---|---|
간단한 문자열 검색 (0) | 2017.04.18 |
임의의 수만큼 연속되는 특정문자 치환. (0) | 2017.04.10 |