简体   繁体   English

跳过x个字符,然后在c#中为每个y个字符插入一个字符串

[英]Skip x characters then insert a string for every y characters in c#

Given a sting, how can I skip the first x characters and then insert a value for every y characters? 给定一个刺,我如何跳过前x个字符,然后为每个y个字符插入一个值?

For example: 例如:

“Lorem ipsum dolor sit amet,” 

when skipping the first 10 caracters and then indsert “[here]” for every 3 caracters becomes: 当跳过前10个角色,然后每3个角色插入“ [here]”时:

“Lorem ipsu[here]m d[here]olo[here]r s[here]it [here]ame[here]t,”

What is the most efficient, fastest way of doing this in C#? 在C#中,最有效,最快的方法是什么?

My current function looks like this but isn't doing the skipping part, I know how to implement the skipping part but the technique used does not seem to be optimal: 我当前的函数看起来像这样,但是没有执行跳过部分,我知道如何实现跳过部分,但是所使用的技术似乎并不是最佳的:

public static string InsertHere(string source)
    {
        if (string.IsNullOrWhiteSpace(source))
        {
            return string.Empty;
        }

        int count = 0;
        var sb = new StringBuilder();
        foreach (char c in source)
        {
            count++;
            sb.Append(c);
            if (count == 10)
            {
                count = 0;
                sb.Append(@"[here]");
            }
        }
        return sb.ToString();
    }

Not sure if it's faster (you really need to benchmark this): 不知道它是否更快(您确实需要对此进行基准测试):

static Regex Rx = new Regex("(^.{10}|.{3})", RegexOptions.Compiled);

//...
var result = Rx.Replace(input, "$0[here]");

Using a static compiled regex for best speed in a regex solution. 在正则表达式解决方案中使用静态编译的正则表达式以获得最佳速度。

You'd have to profile them to see what's best, but here's my effort, I've gone for a string reader into a buffer approach. 您必须对它们进行概要分析才能看到最好的方法,但是这是我的努力,我已将字符串读取器用于缓冲方法。

    public static string InsertStringRepeatedly(string source, int skip, int insertEvery, string toInsert)
    {
        var sb = new StringBuilder();
        using (var sr = new StringReader(source))
        {
            var buffer = new char[Math.Max(skip, insertEvery)];
            var read = sr.Read(buffer, 0, skip);
            sb.Append(buffer, 0, read);
            while (sr.Peek() > 0)
            {
                sb.Append(toInsert);
                read = sr.Read(buffer, 0, insertEvery);
                sb.Append(buffer, 0, read);
            }
        }
        return sb.ToString();
    }

Edit: 编辑:

Fixed for edge cases source.Length < 10 or not whole multiple of 10 + 3*x. 固定于边缘情况源。长度<10或不是10 + 3 * x的整数倍。 Also use just one buffer now. 现在也只使用一个缓冲区。

Usage example: 用法示例:

    private static void Main(string[] args)
    {
        var result = InsertStringRepeatedly("Lorem ipsum dolor sit amet,", 10, 3, "[Here]");
        Console.Write("\"");
        Console.Write(result);
        Console.WriteLine("\""); //quotes to show we dealt with edge cases correctly
        Console.ReadLine();
    }

No need to Regex . 无需Regex Try String methods for more performance: 尝试使用String方法以获得更高的性能:

string str = "Lorem ipsum dolor sit amet,";
var rep = "[here]";

var step = 10 + rep.Length;
while (str.Length > step)
{
    str = str.Insert(step, rep);
    step = step + 3 + rep.Length;
}

I thought I might as well put my modifications into an answer. 我以为我也可以将修改内容作为答案。

I should stress that this may well not be the best way to do it but it is the simplest modification of your code to do it and definitely works... 我应该强调,这可能不是最好的方法,但这是对代码进行的最简单的修改,并且肯定可以工作...

public static string InsertHere(string source, int skip, int repeatPeriod)
{
    if (string.IsNullOrWhiteSpace(source))
    {
        return string.Empty;
    }

    int count = -1*(skip-repeatPeriod);
    var sb = new StringBuilder();
    foreach (char c in source)
    {
        count++;
        sb.Append(c);
        if (count == repeatPeriod)
        {
            count = 0;
            sb.Append(@"[here]");
        }
    }
    return sb.ToString();
}

I'm not very proficient with C# yet (I have a VB background), but the first thing that comes to mind is this: 我还不太熟练C#(我有VB背景),但是想到的第一件事是:

 string WordProcess(string StringToProcess, string InsertValue, byte NumberToProcess )
    {
        string Result = "";
        while (StringToProcess != "")
        {
            for (byte b = 0; b < NumberToProcess; b++)
            {
                string TempString = StringToProcess;
                Result += TempString.Remove(1, TempString.Length - 1);
                StringToProcess = StringToProcess.Remove(0, 1);
            }
            Result += InsertValue;
        }
        return Result;
    }

I've tried it and it seemed to work fine for me, so you should be able to just copy and paste it in and use it as it is. 我已经尝试过了,它似乎对我来说很好用,所以您应该能够将其复制并粘贴并按原样使用。 Not sure how fast it is, but it works at least. 不知道它有多快,但至少可以正常工作。

I hope you find this useful/helpful. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM