프로그래머스_C#/Level_1
[프로그래머스 C#] 문자열을 정수로 바꾸기
최애뎡
2021. 8. 29. 00:10
728x90
반응형
https://programmers.co.kr/learn/courses/30/lessons/12925
코딩테스트 연습 - 문자열을 정수로 바꾸기
문자열 s를 숫자로 변환한 결과를 반환하는 함수, solution을 완성하세요. 제한 조건 s의 길이는 1 이상 5이하입니다. s의 맨앞에는 부호(+, -)가 올 수 있습니다. s는 부호와 숫자로만 이루어져있습니
programmers.co.kr
public class Solution {
public int solution(string s) {
int answer = 0;
int.TryParse(s, out answer);
return answer;
}
}
TryParse로 간단하게
https://docs.microsoft.com/ko-kr/dotnet/api/system.int32.tryparse
Int32.TryParse 메서드 (System)
숫자의 문자열 표현을 해당하는 32비트 부호 있는 정수로 변환합니다.Converts the string representation of a number to its 32-bit signed integer equivalent. 반환 값은 작업의 성공 여부를 나타냅니다.A return value indi
docs.microsoft.com
* TryParse는 out으로 값을 내보냄과 동시에 bool으로 반환 여부를 확인할 수 있기 때문에 자주 사용함
반응형