본문 바로가기
C#

C# String Array convertALL

by Derricks2 2023. 1. 16.
반응형

https://school.programmers.co.kr/learn/courses/30/lessons/12939

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

알고리즘 최솟값, 최댓값 문제를 c#을 사용해 풀었습니다.

풀이 과정에서 문자열의 단어들을 int 값으로 변환해서 비교하는 수행과정이 필요했습니다.

이에 사용한 c# API 정리하려고 합니다. 


C# String 문자열을 -> int Array로 변환하는 과정 

주어진 문제에서는 문자열 안에 단어들이 공백으로 구분 지어져 있었습니다.

Eg, 

string s = "-1 -2 -3 -4";
string[] tokens = s.Split(' ');
int[] convertedItems = Array.ConvertAll<string, int>(tokens, int.Parse);

Array가 가지고 있는 ConvertALL API를 사용하면, 한 번에 변환 가능하다.

https://learn.microsoft.com/en-us/dotnet/api/system.array.convertall?view=netframework-4.8#examples 

 

Array.ConvertAll<TInput,TOutput>(TInput[], Converter<TInput,TOutput>) Method (System)

Converts an array of one type to an array of another type.

learn.microsoft.com

 

반응형

'C#' 카테고리의 다른 글

C# ArrayList 이야기  (0) 2023.02.01
c# 문자열 특정 위치 변경 방법 StringBuilder  (0) 2023.01.17
박싱 언박싱  (0) 2023.01.09