The method used to divide a given string which is separated by the delimiters for split into an array of strings, is called the C# String Split() method, with the delimiters of the split being an array consisting of strings or an array consisting of characters or just characters and an array of strings consisting of the substrings whose delimiters are an array of Unicode characters or elements of the specified string, is returned by using the Split() method in C# and ArgumentOutofRangeException and ArgumentException exceptions are raised as part of exception handling when using this method.
Syntax
The syntax of the C# String Split() method is as follows:
public String[] Split(String[] separator, int count, StringSplitOptions options); public String[] Split(params Char[] character) public String[] Split(Char[], Int32) public String[] Split(Char[], Int32, StringSplitOptions) public String[] Split(Char[], StringSplitOptions) public String[] Split(String[], Int32, StringSplitOptions) public String[] Split(String[], StringSplitOptions)
Where separator is the array of strings delimiting the substrings in the given string
The count keeps count of the maximum number of substrings to be returned
Options can remove the empty entries option to discard the array elements that are empty from the returned array or the options none to include the empty array elements from the returned array.
Working of C# String Split() method
- Whenever there is a need to divide the string based on the delimiter separating the array of strings or array of characters or just characters, we make use of the String split() method.
- The delimiters separating the strings can be an array of strings or an array of characters, or just characters.
- The substring of the given string separated based on the given delimiter is returned by using a string split() method.
- ArgumentOutofRangeException and ArgumentException exceptions are raised as part of exception handling when using the Split() method.
Examples of C# String Split()
Given below are the examples of C# String Split():
Example #1
C# program to demonstrate String Split() method to separate the string separated by a comma
Code:
using System; //a class called check is defined public class check { //main method is called public static void Main(string[] args) { //a string variable is used to store the string consisting of delimiters string str = "Welcome,to,C,Sharp"; //a string array is used to store the array of strings returned by using string split method string[] str2 = str.Split(','); foreach (string s in str2) { Console.WriteLine(s); } } }
Output:
In the above program, a class called check is called. Then the main method is called, within which a string variable is defined to store the string consisting of delimiters which can be used to separate the given string into an array of substrings, and then a string array is defined to store the array of substrings returned by using string split() method which is displayed as output. The output is shown in the snapshot above.
Example #2
C# program to demonstrate string split() method to separate the given string consisting of multiple delimiters into an array of strings:
Code:
using System; //a namespace called program is defined namespace program { //a class called check is defined class check { //main method is called static void Main(string[] args) { //a string variable is used to store the string consisting of multiple delimiters string str1 = "Welcome,to-C|Sharp"; //a string array is defined to store the array of substrings returned by using the split() method on the given string string[] str2 = str1.Split(new char[] { ',', '-', '|' }, StringSplitOptions.RemoveEmptyEntries); for (int j = 0; j <p><strong>Output:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172534786632223.png?x-oss-process=image/resize,p_40" class="lazy" alt="C# String Split()" ></p> <p>In the above program, a namespace called program is created. Then the class called check is defined within which the main method is called. Then a string variable is defined to store the string consisting of multiple delimiters. Then we use the split() method to separate the given string into an array of substrings displayed as the output. The output is shown in the snapshot above.</p> <h4 id="Example">Example #3</h4> <p>C# program to demonstrate String IndexOf() method to separate the given string consisting of multiple delimiters and store the return value in a list:</p> <p><strong>Code:</strong></p> <pre class="brush:php;toolbar:false">using System; using System.Collections.Generic; //a namespace called program is created namespace program { //a class called check is defined class check { //main method is called static void Main(string[] args) { //a string variable is defined to store the string consisting of multiple delimiters string str = "Welcome-to,C|Sharp"; //a list is defined to store the substrings separated from the given string consisting of delimiters IList<string> listname = new List<string>(str.Split(new char[] { '-', ',', '|' }, StringSplitOptions.RemoveEmptyEntries)); for (int j = 0; j <p><strong>Output:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172534786733788.png?x-oss-process=image/resize,p_40" class="lazy" alt="C# String Split()" ></p> <p>In the above program, a namespace called program is created. Then a class called check is defined within which the main method is called. Then a string variable is defined to store the string consisting of multiple delimiters. Then, by using a string split() method, the given string can be split into an array of strings stored in a list by creating a new list. The output is shown in the snapshot above.</p></string></string>
The above is the detailed content of C# String Split(). For more information, please follow other related articles on the PHP Chinese website!

C#.NET is a powerful development platform that combines the advantages of the C# language and .NET framework. 1) It is widely used in enterprise applications, web development, game development and mobile application development. 2) C# code is compiled into an intermediate language and is executed by the .NET runtime environment, supporting garbage collection, type safety and LINQ queries. 3) Examples of usage include basic console output and advanced LINQ queries. 4) Common errors such as empty references and type conversion errors can be solved through debuggers and logging. 5) Performance optimization suggestions include asynchronous programming and optimization of LINQ queries. 6) Despite the competition, C#.NET maintains its important position through continuous innovation.

The future trends of C#.NET are mainly focused on three aspects: cloud computing, microservices, AI and machine learning integration, and cross-platform development. 1) Cloud computing and microservices: C#.NET optimizes cloud environment performance through the Azure platform and supports the construction of an efficient microservice architecture. 2) Integration of AI and machine learning: With the help of the ML.NET library, C# developers can embed machine learning models in their applications to promote the development of intelligent applications. 3) Cross-platform development: Through .NETCore and .NET5, C# applications can run on Windows, Linux and macOS, expanding the deployment scope.

The latest developments and best practices in C#.NET development include: 1. Asynchronous programming improves application responsiveness, and simplifies non-blocking code using async and await keywords; 2. LINQ provides powerful query functions, efficiently manipulating data through delayed execution and expression trees; 3. Performance optimization suggestions include using asynchronous programming, optimizing LINQ queries, rationally managing memory, improving code readability and maintenance, and writing unit tests.

How to build applications using .NET? Building applications using .NET can be achieved through the following steps: 1) Understand the basics of .NET, including C# language and cross-platform development support; 2) Learn core concepts such as components and working principles of the .NET ecosystem; 3) Master basic and advanced usage, from simple console applications to complex WebAPIs and database operations; 4) Be familiar with common errors and debugging techniques, such as configuration and database connection issues; 5) Application performance optimization and best practices, such as asynchronous programming and caching.

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

C# and .NET are suitable for web, desktop and mobile development. 1) In web development, ASP.NETCore supports cross-platform development. 2) Desktop development uses WPF and WinForms, which are suitable for different needs. 3) Mobile development realizes cross-platform applications through Xamarin.

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
