char indexof

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;

public static class EnumerableExtension
{
	public static int FirstIndexMatch<TItem>(this IEnumerable<TItem> items, Func<TItem, bool> matchCondition)
	{
		var index = 0;
		foreach (var item in items)
		{
			if (matchCondition.Invoke(item))
			{
				return index;
			}

			index++;
		}

		return -1;
	}
}

public class Program
{
	public static void Main()
	{
		var test = new[]
		{
		'\0', '\0', '\0', '\0', '1', '2', '3', '\0', '5'
		}

		;
		var s = new string (test);
		Console.WriteLine("{0} -> length: {1}", s, s.Length);
		Console.WriteLine("First non-NUL at: {0}", test.FirstIndexMatch(ch => ch != '\0'));
	}
}
;