Elias RC
Elias RC

Reputation: 89

How to split by multiple strings in C#

My Question consists of how might i split a string like this:

""List of devices attached\r\n9887bc314\tdevice\r\n12n1n2nj1jn2 
\tdevice\r\n\r\n"

Into:

[n9887bc314,n12n1n2nj1jn2]

I have tried this but it throws the error "Argument 1: cannot convert from 'string' to 'char'"

string[] delimiterChars = new string[] {"\\","r","n","tdevice"};
string y = output.Substring(z+1);
string[] words;
words = y.Split(delimiterChars, StringSplitOptions.None);

I'm wondering if i'm doing something wrong because i'm quite new at c#. Thanks A Lot

Upvotes: 5

Views: 7651

Answers (4)

LANimal
LANimal

Reputation: 81

First of all String.Split accept strings[] as delimiters

Here is my code, hope it will helps:

string input = "List of devices attached\r\n9887bc314\tdevice\r\n12n1n2nj1jn2\tdevice\r\n\r\n";
string[] delimiterChars = { 
                            "\r\n",
                            "\tdevice",
                            "List of devices attached"
                          };
var words = input.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
foreach (var word in words)
{
    Console.WriteLine(word);
}
        

Upvotes: 7

Juan G Carmona
Juan G Carmona

Reputation: 2208

I've been doing a first aproach and it works, it might help: I splited the input looking for "/tdevice" and then cleaned everyting before /r/n including the /r/n itself. It did the job and should work with your adb output.

EDIT: I've refactored my answer to consider @LANimal answer (split using all delimiters) and I tried this code and works. (note the @ usage)

static void Main(string[] args)
    {
        var inputString = @"List of devices attached\r\n9887bc314\tdevice\r\n12n1n2nj1jn2\tdevice\r\n\r\n";

        string[] delimiters =  {
                                @"\r\n",
                                @"\tdevice",
                                @"List of devices attached"
                              };
        var chunks = inputString.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

        string result = "[";

        for (int i = 0; i < chunks.Length; i++)
        {              
                result += chunks[i] + ",";
        }
        result = result.Remove(result.Length - 1);
        result += "]";

        Console.WriteLine(result);
        Console.ReadLine();
    }

I hope it helps,

Juan

Upvotes: 1

CodingYoshi
CodingYoshi

Reputation: 27039

Split the whole string by the word device and then remove the tabs and new lines from them. Here is how:

var wholeString = "List of devices attached\r\n9887bc314\tdevice\r\n12n1n2nj1jn2\tdevice\r\n\r\n";
var splits = wholeString.Split(new[] { "device" }, StringSplitOptions.RemoveEmptyEntries);
var device1 = splits[1].Substring(splits[1].IndexOf("\n") + 1).Replace("\t", "");
var device2 = splits[2].Substring(splits[2].IndexOf("\n") + 1).Replace("\t", "");

Upvotes: 1

chaosifier
chaosifier

Reputation: 2964

You could do:

string str = @"List of devices attached\r\n9887bc314\tdevice\r\n12n1n2nj1jn2\tdevice\r\n\r\n";    
string[] lines = str.Split(new[] { @"\r\n" }, StringSplitOptions.None);
string firstDevice = lines[1].Replace(@"\tdevice", "");
string secondDevice = lines[2].Replace(@"\tdevice", "");

Upvotes: 0

Related Questions