Separate a string / string using a delimiter in c #

0

My question is basically how I can separate a string using a delimiter. In my case what I did was split it using a split, let's say the chain will contain the following:

ADELIMITADORB

But what I get by separating it is DELIMITADORB when what I wanted to get was A Y B . Result I get:

Source:

using System;

namespace Consol
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            // TODO: Implement Functionality Here
            //obtenemos la string
            string str = "AdelimitadorB";

            char[] delimiterChars = { 'D', 'E', 'L', 'I', 'M' , 'I', 'T', 'A', 'D', 'O', 'R'};

            string[] arr = str.Split(delimiterChars);

            string a = arr[0];
            string b = arr[1];

            Console.Write(b);
            Console.ReadKey(true);
        }
    }
}

How you could do to get A Y B separately using the DELIMITADOR delimiter.

    
asked by Sergio Ramos 10.05.2017 в 13:15
source

2 answers

2

The problem with your code is that you are using delimiters in capital letters and that you use loose characters instead of a word. Try this:

string str = "AdelimitadorB"; 
string[] arr=str.Split(new string[] { "delimitador" }, 
                                                StringSplitOptions.None);
string a = arr[0];
string b = arr[1];

If you insist on defining your delimiter by separating the letters as you do, you can do the following:

char[] delimiterChars = { 'd', 'e', 'l', 'i', 'm', 'i', 't', 'a', 'd', 'o', 'r' };
string[] arr=str.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);

Although this does not really make you split by the whole word, if not every time it will separate you every time you find one of the characters of the word.

Anyway, I do not recommend using a whole word as a delimiter, if not a single character

    
answered by 10.05.2017 / 13:20
source
0

You could use this solution that I found in: ( link )

string[] arr = str.Split(new[] { "delimitador" },StringSplitOptions.None);
    
answered by 10.05.2017 в 13:21