Java 18일차 6 (문자열 메소드)

2022. 12. 5. 11:52코딩배움일지/JAVA

package j21_문자열메소드;

import java.util.function.Function;

public class StringMethod3 {

	public static void main(String[] args) {
		
		Function<String,String> replacePhoneToken = phoneNumber -> 
		phoneNumber.replaceAll(" ", "").replaceAll("/", "").replaceAll("-", "").replaceAll("[.]","");/*정규식에서 [.]*/
		
		String phone = "010-4142-3421";
		
//		String phone1 = "010-9988/1916";
//		String phone2 = "010-9988/1916";
//		String phone3 = "010-9988/1916";
//		String phone4 = "010-9988/1916";
//		
//		phone1.replaceAll(" ", "").replaceAll("/", "").replaceAll("-", "").replaceAll("[.]","");
//		phone2.replaceAll(" ", "").replaceAll("/", "").replaceAll("-", "").replaceAll("[.]","");
//		phone3.replaceAll(" ", "").replaceAll("/", "").replaceAll("-", "").replaceAll("[.]","");
//		phone4.replaceAll(" ", "").replaceAll("/", "").replaceAll("-", "").replaceAll("[.]","");
//		
//		System.out.println(replacePhoneToken.apply(phone1));
//		System.out.println(replacePhoneToken.apply(phone2));
//		System.out.println(replacePhoneToken.apply(phone3));
//		System.out.println(replacePhoneToken.apply(phone4));
		
		
		System.out.println(replacePhoneToken.apply(phone));
		
//		System.out.println(phone.replaceAll("-","")); /* - 을 공백으로 바꿔라*/
		
//		phone = phone.replaceAll("-","");
//		
//		phone = phone.replaceAll("/","");
		
//		System.out.println(phone);
		
		
		
	
	}
}

 

 

import java.util.Arrays;
import java.util.List;

public class StringMethod4 {

	public static void main(String[] args) {
		
		String roles = "ROLE_USER, ROLE_MANAGER, ROLE_ADMIN, GUEST";		
		roles = roles.replaceAll(" ", "");
		
		String[] roleArray = roles.split(",");		
		List<String> roleList = Arrays.asList(roleArray);
		
		System.out.println(roleList);
		
		roleList.forEach(role ->{
			/*prefix(접두어) startsWith */
			/*suffix(접미어) endsWith */
			
			if(role.startsWith("ROLE_") && role.equalsIgnoreCase("role_user")) { /*ROLE_ 이것으로 시작하는 지를 묻는다.*//*equalsIgnoreCase 소대문자 구분없이 글자로만 */
				System.out.println("권한: " + role);
			}
			
		});
	
		
	}
}