๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ปTech/โ˜•Java

[Java] ํœด๋Œ€ํฐ ๋ฒˆํ˜ธ ๋งˆ์Šคํ‚น ์ฝ”๋“œ

by _viper_ 2019. 6. 12.
๋ฐ˜์‘ํ˜•

ํœด๋Œ€ํฐ ๋ฒˆํ˜ธ๋ฅผ asterisk(*)๋กœ ์•”ํ˜ธํ™”ํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

	/**
	 * encrypt number in text
	 * 
	 * maskingCallNumber
	 *  
	 * @param str
	 * @return String
	 */
	public static String maskingCallNumber(String str){
		String replaceString = str;
		String matchedStr ="";

		String pattern ="(\\d{2,3})-?(\\d{3,4})-?(\\d{3,4})";
		Matcher matcher = Pattern.compile(pattern).matcher(str);

		if(matcher.find()){
			StringBuffer br = new StringBuffer();
			for(int i = 1 ; i<=matcher.groupCount(); i++){
				matchedStr = matcher.group(i);
				for(int j = 0 ; j <matchedStr.length();j++){
					br.append("*");
				}
				if(i !=matcher.groupCount()){
					br.append("-");
				}
			}
			replaceString =matcher.replaceAll(br.toString());
		}
		return replaceString;
	}