반응형
단일 char 구분자로 문자열 분할
string words = "123,456,789";
string[] split = words.Split(new char[] {','});
foreach(string word in split)
System.Console.WriteLine(word);
, 를 기준으로 문자열 분할됨.
복수 char 구분자로 문자열 분할
string words = "123,456,789.abc.def.ghi";
string[] split = words.Split(new char[] { ',', '.' });
foreach (string word in split)
System.Console.WriteLine(word);
, 과 . 를 기준으로 문자열 분할됨.
분할 배열 개수 지정
string words = "123,456,789.abc.def.ghi";
string[] split = words.Split(new char[] { ',', '.' }, 4);
// 총 4개의 배열 생성
foreach (string word in split)
System.Console.WriteLine(word);
배열이 4개 생성됨.
구분 시 빈 배열 포함 여부 지정
string words = "a,b,c,,e,f,g";
// StringSplitOptions.None 배열 포함 옵션
string[] split = words.Split(new char[] { ',', '.' }, StringSplitOptions.None);
// 빈 배열 포함
System.Console.WriteLine("빈배열 포함");
foreach (string word in split)
System.Console.WriteLine($" : {word}");
// StringSplitOptions.RemoveEmptyEntries 배열 미포함 옵션
split = words.Split(new char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);
// 빈 배열 제외
System.Console.WriteLine("빈배열 제외");
foreach (string word in split)
System.Console.WriteLine($" : {word}");
첫번째 분할에는 빈 배열 포함.
두번째 분할에는 빈 배열 미포함.
string 구분자로 문자열 분할
string words = "123and456and789";
string[] split = words.Split(new string[] { "and"}, StringSplitOptions.None);
foreach (string word in split)
System.Console.WriteLine($" : {word}");
and 구분자를 기준으로 문자열이 구분됨.
반응형
'C#' 카테고리의 다른 글
[C#] 매개변수 배열 (params) (0) | 2023.10.11 |
---|---|
[C#] in 매개변수 (1) | 2023.10.10 |
[C#] out 매개변수 (2) | 2023.10.10 |
[C#] 참조 매개변수 ref (0) | 2023.10.10 |
[C#] 파일 삭제 (0) | 2023.10.07 |