ProgramingTip

여러 문자 구분 기호를 기반으로 분할 분할

bestdevel 2020. 12. 7. 20:30
반응형

여러 문자 구분 기호를 기반으로 분할 분할


"4,6,8 \ n9,4"문자열이 있습니다.

을 (를) 기준으로 분할하고 싶습니다. \ n

출력 배열은

4
6
8
9
4

편집하다 :

이제 콘솔에서 해독을 가능합니다. 위와 같이 콘솔에 노드를 입력하면 디렉토리 코드에서 "4,6,8\\n9,4". 이제 "," and "\\n". 표현은 어떻게 바꾸나요?


사용 사항 String.split (문자 [])

string strings = "4,6,8\n9,4";
string [] split = strings .Split(new Char [] {',' , '\n' });

편집하다

불필요한 빈 항목이 있으면 다음을 시도하십시오. String.Split 메서드 (String [], StringSplitOptions)

string [] split = strings .Split(new Char [] {',' , '\n' }, 
                                 StringSplitOptions.RemoveEmptyEntries);

EDIT2

이 업데이트 된 질문에 적용됩니다. 필요한 모든 분할 문자를 char [].

string [] split = strings.Split(new Char[] { ',', '\\', '\n' },
                                 StringSplitOptions.RemoveEmptyEntries);

또 다른 옵션은 Regex.Split 을 사용하는 것 입니다. 이것은 분할 시퀀스가 ​​더 복잡한 경우에 유용합니다. 예를 들어 공백이 다음과 같은 분할 구분 기호의 일부일 수도 있습니다.

"4,6,8 , 9\\n\\n4"

그때 :

using System.Text.RegularExpressions;
var i = "4,6,8 , 9\n\n4";
var o = Regex.Split(i, @"[,\s\n]+");
// now o is:
// new string[] { "4", "6", "8", "9" }

사용 된 정규식은 "더 많이 받음"입니다. \ n 사이의 빈 "공백"을 무시하고 "4 6 8 9 4"를 그대로입니다. 따라서 위의 내용은 요점을 표시합니다. 고양이 가죽을 벗기는 한 가지 방법보다.

즐거운 코딩입니다.


var s = "4,6,8\n9,4";
var split = s.Split(new char[]{',', '\n'});

하지만 이건 속임수 야 ...

편집 : 주석 처리.

이 코드 :

static void Main(string[] args)
{
    var s = "4,6,8\n9,4";

    foreach (var a in s.Split(new char[] { ',', '\n' }))
        System.Diagnostics.Debug.WriteLine(a);
}

다음을 출력합니다.

4
6
8
9
4

편집 : 콘솔에서 입력을 읽는 것이 좋습니다. \n수동으로 입력하면 입력합니다.

static void Main(string[] args)
{
    var s = "4,6,8\\n9,4";

    foreach (var a in s.Split(new string[] { ",", "\\n" }, StringSplitOptions.RemoveEmptyEntries))
        System.Diagnostics.Debug.WriteLine(a);
}

string tosplit = "4,6,8\n9,4";
var split = tosplit.Split(new Char [] {',', '\n' });

제대로 인쇄 / 보이지 않는 경우 :

split.ToList().ForEach(Console.WriteLine);

string.Replace ( '\ n', ',') 다음에 string.split ( ',') 할 수 있습니까?

참고 URL : https://stackoverflow.com/questions/7605785/splitting-a-string-based-on-multiple-char-delimiters

반응형