블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2737)
Unity3D (817)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (58)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (53)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday
04-26 00:00

'2019/04/18'에 해당되는 글 1건

  1. 2019.04.18 [펌] c# Split (문자열 분할, 자르기)

(http://link2me.tistory.com 에서 발췌)

기타 : split를 string문자열로 자를때 word_1.Split(new string[] {"\\"}, StringSplitOptions.None) C# Split함수의 경우 직접 문자열을 구분자로 줄수 없다.

Split 은 기본적으로 문자를 입력 받도록 되어 있다. 문자열을 char[]형식으로 변경 후 사용 가능하다.

 

string str = "서울시/서초구/양재동";
string[] result = str.Split(new char[] { '/' });  // /   열 result  라.
for (int i = 0; i < result.Length ; i++)  // 은 0  며,   
{
    MessageBox.Show(i + "번째 배열 ==> " + result[i]);
}

 

string str = "jsk005@naver.com";
char[] sp ="@".ToCharArray();
string[] result = str.Split(sp);
for (int i = 0; i < result.Length; i++)
{
    MessageBox.Show(i + "번째 배열 ==> " + result[i]);
}

 

string str = "홍 길 동";
string[] result = str.Split(new char[] {' '});  // new char[] {}  도 공백으로 나눈다는 의미
for (int i = 0; i < result.Length; i++)
{
    MessageBox.Show(i + "번째 배열 ==> " + result[i]);
}

 

위의 예제와 아래 예제의 차이점을 보면 split  자(char)  열(string)이다. 

string 으로 할 경우에는 반드시 StringSplitOptions.None 또는 StringSplitOptions.RemoveEmptyEntries 까지 추가해줘야만 에러가 발생하지 않는다. 물론 위의 것도 StringSplitOptions 를 추가해줘도 된다.

 

string str = "홍길동~~~이순신~~~강감찬~~~을지문덕";
string[] result = str.Split(new string[] {"~~~"}, StringSplitOptions.None);
for (int i = 0; i < result.Length; i++)
{
    MessageBox.Show(i + "번째 배열 ==> " + result[i]);
}

string[] separator = new string[1] { "\r\n\r\n" };  //분리할 기준 문자열
string[] strResult = s.Split(separator, StringSplitOptions.RemoveEmptyEntries);

  

* StringSplitOptions.None  //     

* StringSplitOptions.RemoveEmptyEntries  // 반환값에 빈 부분 문자열 포함 안됨

 

foreach (string strValue in strResult)
{
    
}

또는 

for (var i =0 ; i < strResult.Length ; i++) 

{

}

 

//구     

string str = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]";
string[] stringSeparators = new string[] { "[stop]" };
string[] result = str.Split(stringSeparators, StringSplitOptions.None);

for (int i = 0; i < result.Length; i++)
{
    if (String.IsNullOrEmpty(result[i]))  //  null  
    {
        MessageBox.Show("null" + result[i]);
    }
    else
    {
        MessageBox.Show(i + "번째 배열 ==> " + result[i]);
    }                
} 

 

아래와 같이StringSplitOptions.RemoveEmptyEntries 으로 수정하면 결과가 어떻게 나올까?

string str = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]";
string[] stringSeparators = new string[] { "[stop]" };
string[] result = str.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);  

// 반환값에 빈 부분 문자열 포함 안됨
for (int i = 0; i < result.Length; i++)
{
    if (String.IsNullOrEmpty(result[i]))
    {
        MessageBox.Show("null" + result[i]);
    }
    else
    {
        MessageBox.Show(i + "번째 배열 ==> " + result[i]);
    }                
} 

결과는 null 이나 공백은 무시되므로 배열은 0, 1, 2 만 추출된다.



   

System.Text.RegularExpressions.Regex.Split(자를 문자열, 정규식 문자열)

using System;
using System.Text.RegularExpressions;

 

string str = "cat\r\ndog\r\nanimal\r\nperson";
string[] result = Regex.Split(str, "\r\n");
for (int i = 0; i < result.Length; i++)
{
    MessageBox.Show(i + "번째 배열 ==> " + result[i]);
} 



포함된 문자열의 개수 구하기
int
 count = source.Split('/').Length - 1;

 

[출처] http://hunq.blogspot.com/2016/09/c-split.html

반응형
Posted by blueasa
, |