[Java] - 자바 전화번호에 하이픈(-) 넣는 방법

[Java] - 자바 전화번호에 하이픈(-) 넣는 방법

반응형

#자바 전화번호에 하이픈(-) 추가하는 메서드

public class test { public static String convertTelNo(String src) { String mobTelNo = src; if (mobTelNo != null) { // 일단 기존 - 전부 제거 mobTelNo = mobTelNo.replaceAll(Pattern.quote("-"), ""); if (mobTelNo.length() == 11) { // 010-1234-1234 mobTelNo = mobTelNo.substring(0, 3) + "-" + mobTelNo.substring(3, 7) + "-" + mobTelNo.substring(7); } else if (mobTelNo.length() == 8) { // 1588-1234 mobTelNo = mobTelNo.substring(0, 4) + "-" + mobTelNo.substring(4); } else { if (mobTelNo.startsWith("02")) { // 서울은 02-123-1234 mobTelNo = mobTelNo.substring(0, 2) + "-" + mobTelNo.substring(2, 5) + "-" + mobTelNo.substring(5); } else { // 그외는 012-123-1345 mobTelNo = mobTelNo.substring(0, 3) + "-" + mobTelNo.substring(3, 6) + "-" + mobTelNo.substring(6); } } } return mobTelNo; } public static void main(String[] args) { System.out.println(convertTelNo("05123421232")); // 051-2342-1232 System.out.println(convertTelNo("0223421232")); // 02-234-2123 System.out.println(convertTelNo("15882342")); // 1588-2342 } }

728x90

반응형

from http://pingfanzhilu.tistory.com/326 by ccl(A) rewrite - 2021-09-29 14:27:16