[c#] string(문자열)을 array(배열)로 잘라서 배치하는 방법

in #kr-coding6 years ago

string을 특정 조건에 맞춰서 잘라야할 때가 있습니다.
보통 저장했던걸 불러오거나 서버에서 받아온 string을 array로 전환하는데 사용됩니다.


public string str;
public string[] array;

를 준비합니다.

str = "a,b,c,d";
일 경우

array = str.Split(',');
를 실행해서 Split(char형의 조건)함수가 str을 잘라내면

array[0] = "a";
array[1] = "b";
array[2] = "c";
array[3] = "d";

의 결과가 도출됩니다.

잘라내는 기준은 char형태의 ','입니다.
잘라내는 조건이 string이 아니기 때문에 ","가 아닌 ','를 사용하시면 됩니다.
잘라내는 조건은 굳이 ','가 아니더라도 '/'나 'x'로도 가능합니다.
잘라내야할 원본 string의 양식에 맞춰서 조건을 부여해주면 됩니다.

array는 따로 new string으로 배열을 확보하지 않아도 Split()이 알아서 배열을 확장시킵니다.

Sort:  

Congratulations @betdon! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

New japanese speaking community Steem Meetup badge
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.033
BTC 64006.33
ETH 3077.08
USDT 1.00
SBD 3.87