>백엔드 개발 >C#.Net 튜토리얼 >문자열을 분할하고 연결하는 C# 프로그램

문자열을 분할하고 연결하는 C# 프로그램

王林
王林앞으로
2023-08-28 23:13:101341검색

C# 程序来分割和连接字符串

C#에서 문자열을 분할하고 결합하려면 Split() 및 Join() 메서드를 사용하세요. 아래 문자열이 있다고 가정합니다. -

string str = "This is our Demo String";

문자열을 분할하려면 분할() 메서드를 사용합니다. -

var arr = str.Split(' ');

이제 결합하려면 Join() 메서드를 사용하고 나머지 문자열을 결합합니다. 여기서는 Skip() 메서드를 사용하여 문자열의 일부를 건너뜁니다. -

string rest = string.Join(" ", arr.Skip(1));

Example

C#에서 다음 코드를 실행하여 문자열을 분할하고 연결할 수 있습니다.

실시간 데모

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
   class MyApplication {
      static void Main(string[] args) {
         string str = "This is our Demo String";
         var arr = str.Split(' ');
         // skips the first element and joins rest of the array
         string rest = string.Join(" ", arr.Skip(1));
         Console.WriteLine(rest);
      }
   }
}

Output

is our Demo String

위 내용은 문자열을 분할하고 연결하는 C# 프로그램의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제