How to split string by multiple character delimiter in C#

2 Answers

0 votes
using System;

class SplitStringByMultipleCharacterDelimiter_CSharp
{
    static void Main()
    {
        string s = "c#!java,@c!php*,python";

        char[] sp = {',', '@', '!', '*'};
        string[] arr = s.Split(sp, StringSplitOptions.RemoveEmptyEntries);
        
        foreach (string word in arr) {
            Console.WriteLine(word);
        }
    }
}


/*
run:
       
c#
java
c
php
python
    
*/

 



answered Jul 26, 2018 by avibootz
edited Sep 14, 2024 by avibootz
0 votes
using System;

class SplitStringByMultipleCharacterDelimiter_CSharp
{
    static void Main()
    {
        string s = "c#!java,@c!php*,python";

        string[] arr = s.Split(new string[] { ",", "@", "!", "*" }, StringSplitOptions.RemoveEmptyEntries);
        
        foreach (string word in arr) {
            Console.WriteLine(word);
        }
    }
}


/*
run:
       
c#
java
c
php
python

*/

 



answered Jul 26, 2018 by avibootz
edited Sep 14, 2024 by avibootz

Related questions

1 answer 128 views
1 answer 113 views
1 answer 115 views
1 answer 115 views
1 answer 1,705 views
1 answer 102 views
1 answer 59 views
...