프로그래머스 #JAVA(2)
-
자릿수 더하기
문제 1) String으로 형 변환 1-1) 1 2 3 4 5 6 7 8 9 10 11 12 13 public class Solution { public int solution(int n) { String str = ""+n; String[] strarr = str.split(""); int temp = 0; for(String s : strarr){ temp += Integer.parseInt(s); } return temp; } } Colored by Color Scripter cs 1-2) valueOf()를 이용한 형 변환 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import java.util.*; import static java.lang.String.valueOf; pu..
2023.01.19 -
replace vs replaceAll
replace(찾을문자열, 바꿀문자열) 주어진 문자열에서 바꾸고 싶은 특정 문자열이 있을 때 사용하는 함수이다. 1 2 3 4 String str = "ababab"; String result1 = str.replace("ab", "0"); //000 Colored by Color Scripter cs 같은 문자열에 replaceAll을 적용해보겠다. 1 2 3 4 String str = "ababab"; String result2 = str.replaceAll("ab", "0"); //000 Colored by Color Scripter cs replaceAll을 사용했을 때도 replace와 같은 결과가 나왔다. 그렇다면 둘의 차이는 무엇일까? replaceAll(정규식 또는 찾을문자열, 바꿀문자열)..
2023.01.18