using System;
using System.Text;
namespace Sample
{
internal static class StringExtentions
{
public static string CustomReplace(this string origin, string from, string to, StringComparison stringComparison = StringComparison.Ordinal)
{
var sb = new StringBuilder();
int position = 0;
do
{
var quoteStart = origin.IndexOf('"', position);
if (quoteStart == -1)
quoteStart = origin.Length;
var headLength = quoteStart - position;
if (headLength > 0)
{
sb.Append(origin.Substring(position, headLength).Replace(from, to, stringComparison));
}
position = quoteStart;
if(position == origin.Length) break;
var quoteEnd = origin.IndexOf('"', position + 1);
if (quoteEnd == -1)
quoteEnd = origin.Length;
var tailLength = quoteEnd + 1 - position;
if (tailLength > 0)
{
sb.Append(origin.Substring(position, tailLength));
}
position = quoteEnd + 1;
} while (position < origin.Length);
return sb.ToString();
}
}
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Я сказал: \"БАНАН не банан\", но банан не согласен со мной. Но я отвечу этому банану: \"бананством\"".CustomReplace("банан", "яблоко", StringComparison.OrdinalIgnoreCase));
}
}
}
int[] array = {1, 1, 1, 2, 3, 3, 4, 4, 4, 4, 5, 6, 6, 7, 8};
var sortedByFrequency = array
.GroupBy(o => o)
.OrderByDescending(o => o.Count())
.ThenByDescending(o => o.Key)
.SelectMany(o => o)
.ToArray();
Console.WriteLine(string.Join(",",sortedByFrequency.Select(o => o.ToString())));
//4,4,4,4,1,1,1,6,6,3,3,8,7,5,2
Кто нибудь знает как .NET ведет себя в плане потребления ресурсов?
internal class PackageInfo : PackageInfo<FrameType, Frame>
{
public PackageInfo(FrameType key, Frame body) : base(key, body)
{
}
}
internal class ReceiveFilter : FixedHeaderReceiveFilter<PackageInfo>
{
public ReceiveFilter(int headerSize) : base(headerSize)
{
}
public override PackageInfo ResolvePackage(IBufferStream bufferStream)
{
//реализация получения пакета из потока
}
protected override int GetBodyLengthFromHeader(IBufferStream bufferStream, int length)
{
var frameLength = bufferStream.ReadInt32(true);
return frameLength - HeaderSize;
}
}
var client = new EasyClient<PackageInfo>();
client.Initialize(new ReceiveFilter(4));
client.NoDelay = true;
client.ReceiveBufferSize = 10240;
client.NewPackageReceived += NewPackageReceived;