How to split a string into 3rd appearance char C #

I was wondering if there is a way to split the string into the 3rd occurrence of char? When splitting, I used earlier:

line.Substring(line.LastIndexOf(']') +1);

I did not understand that some of my lines have extra square brackets than others, so I have to split into the 3rd place of occurrence of ']', since this is the same position for each line.

Login: [Wed Dec 17 14:40:28 2014] [error] [client 143.117.101.166] File does not exist:

Conclusion:

[Wed Dec 17 14:40:28 2014] [error] [client 143.117.101.166]

The file does not exist:

+4
source share
9 answers

you need to split the string first, then take the 3rd index]

line.Substring(line.IndexOf(line.Split(']')[3]));

or simpler since you said the third index is the same, lock it

line.Substring(59);
+3
source

:

\[[^\]]*\]\s*\[[^\]]*\]\s*\[[^\]]*\]

- escape-, : [ + ] + ], .

var s = "[Wed Dec 17 14:40:28 2014] [error] [client 143.117.101.166] File does not exist:";
var r = new Regex(@"(\[[^\]]*\]\s*\[[^\]]*\]\s*\[[^\]]*\])(.*)$");
var m = r.Match(s);
if (m.Success) {
    Console.WriteLine("Prefix: {0}", m.Groups[1]);
    Console.WriteLine("Error: {0}", m.Groups[2]);
}

+5

Regex , []

string input = " [Wed Dec 17 14:40:28 2014] [error] [client 143.117.101.166] File does not exist";
var regex = new Regex("\\[(.*?)\\]");
var matches = regex.Matches(input);
foreach (var match in matches) // e.g. you can loop through your matches like this
{
   //yourmatch
}
+2

Split:

var items = line.Split(new[]{']','['},StringSplitOptions.RemoveEmptyEntries);
if (items.Count > 3) { 
   /* use items[2], or whatever you need... */ 
}

, .

0

, , # , . - , .

, , , . , REGEX .

, ], [

, , ], , [, .

, , .

, .

0

, , , , :

static class ExtMethods
{
    public static IEnumerable<int> IndexesOf(this string str, char c)
    {
        for (int i = 0; i < str.Length; i++)
        {
            if (str[i] == c)
            yield return i;
        }
    }
}

] - :

int thirdPos = str.IndexesOf(']').Take(3).Last();
0

LINQ:

string input = "[Wed Dec 17 14:40:28 2014] [error] [client 143.117.101.166] File does not exist:";

var result = input.Select((ch, ind) => new { ch, ind }).Where(x => x.ch == ']').Skip(2).FirstOrDefault();

string output = input.Substring(result.ind + 1);
0

Linq:

( , , String .)

static void Main(string[] args)
{
    string test = "[Wed Dec 17 14:40:28 2014] [error] [client 143.117.101.166] File does not exist:";

    var result = SplitOnChar(test, ']', 3);

    Debug.WriteLine(result.Item1);
    Debug.WriteLine(result.Item2);
}

static Tuple<string, string> SplitOnChar(string input, char c, int occurrence)
{
    int charCount = 0;
    string firstPart = new String(input.TakeWhile(x => (x == ']' ? charCount++ : charCount) < 3).ToArray());
    string lastPart = input.Substring(firstPart.Length);
    return Tuple.Create(firstPart, lastPart);
}

: LINQ, - ,

0

:

public static class SplitExtension
{
    public static string[] Split(this string self, char separator, int occurrence)
    {
        return self.Split(new string(separator, 1), occurrence);
    }

    public static string[] Split(this string self, string separator, int occurrence)
    {
        string[] chunks = self.Split(new[] { separator }, StringSplitOptions.None);
        string firstPart = string.Join(separator, chunks.Take(occurrence)) + separator;
        string secondPart = string.Join(separator, chunks.Skip(occurrence));
        return new string[] { firstPart, secondPart };
    }
}

... :

string input = "[Wed Dec 17 14:40:28 2014] [error] [client 143.117.101.166] File does not exist";
string[] output = input.Split(']', 3);

// output[0] = "[Wed Dec 17 14:40:28 2014] [error] [client 143.117.101.166]";
// output[1] = " File does not exist";
0

Source: https://habr.com/ru/post/1674829/


All Articles