2

在我的服务器中运行程序(信标 检测代码)时,我将收到一个可变大小的字符串,名为io385

字符串长度可以根据检测到的信标数量而有所不同:

  • 一个信标:46 字节(前 4 字节无用,接下来 40 字节很重要,接下来 2 字节无用);
  • 两个信标:90 个字节(前 2 个字节无用,接下来 40 个很重要,接下来 2 个无用);
  • 三个信标:134 个字节(前 2 个字节无用,接下来 40 个重要,接下来 2 个无用;

...

所以,有了这个,我的想法是删除任何无用的东西。即使字符串的大小可能不同,我总是想删除固定位置的字节(对于第一个信标,四个和最后两个;对于下一个信标,两个和最后两个)。

我开始手动删除 2信标字符串上的无用字节。但是,我想对此进行优化,以便无论字符串是 46 字节还是 xxxx 字节都可以自动工作(否则,我将不得不为每个可能的字符串长度手动编码字符删除过程)。

        string io385 = "11210000AAAA0000AAAA0000AAAA0000AAAA0A0A0A0ABF210000BBBB0000BBBB0000BBBB0000BBBB0B0B0B0BBF";
        string informacao = String.Copy(io385);
        
        informacao = informacao.Remove(0,4).Remove(40,2).Remove(40,2).Remove(80,2);
        
        
        int x = io385.Length;
        int y = informacao.Length;
        Console.WriteLine("Original String: {0}", io385); 
        Console.WriteLine("Copied String: {0}", informacao);
        Console.WriteLine("Original String length: {0}", x);
        Console.WriteLine("Copied String length: {0}", y);
4

2 回答 2

3

给定

public static IEnumerable<string> GetStuff(string input)
{
    Console.WriteLine(input.Length);
    if (input.Length == 46)
        yield return input.Substring(4, 40);
    else
        for (var i = 0; i < input.Length; i += 44)
            yield return input.Substring(i + 2, 40);
}

用法

var input = "xxxx1234567890123456789012345678901234567890xx";
var input2 = "xx1234567890123456789012345678901234567890xxxx1234567890123456789012345678901234567890xxxx1234567890123456789012345678901234567890xxxx1234567890123456789012345678901234567890xx";
Console.WriteLine(string.Join("\r\n", GetStuff(input)));
Console.WriteLine();
Console.WriteLine(string.Join("\r\n", GetStuff(input2)));

输出

46
1234567890123456789012345678901234567890

176
1234567890123456789012345678901234567890
1234567890123456789012345678901234567890
1234567890123456789012345678901234567890
1234567890123456789012345678901234567890

完整的演示在这里

于 2020-09-09T22:52:50.490 回答
0

一种方法是首先删除前两个字符,以便字符串中的所有“信标”长度相同(44 个字符)。现在我们可以创建一个循环,在其中我们从0length / 44跳过iteration * 44字符(即跳过以前的信标),然后跳过2更多字符(即此信标的前导 2),然后取40字符(我们关心的字符数) .

在一个方法中,这可能看起来像这样:

public static string RemoveUselessCharacters(string input)
{
    // Add argument validation first
    if (string.IsNullOrWhiteSpace(input) || input.Length < 46) return input;

    // Just remove the first two characters right away
    input = input.Substring(2);

    // This will hold the result
    var result = new StringBuilder();

    // Loop once for every beacon
    for (int i = 0; i < input.Length / 44; i++)
    {
         // Skip previous beacons plus two characters, then take 40 characters
        result.Append(string.Concat(input.Skip(i * 44 + 2).Take(40)));
    }

    // Return just the beacon charcters for all the beacons
    return result.ToString();
}

如果您想修改代码以返回 a List<string>,其中每个字符串都是一个单独的信标,那么很容易做到:

public static List<string> GetBeacons(string input)
{
    if (string.IsNullOrWhiteSpace(input) || input.Length < 46) 
        return new List<string> {input};

    input = input.Substring(2);
    var result = new List<string>();

    for (int i = 0; i < input.Length / 44; i++)
    {
        result.Add(string.Concat(input.Skip(i * 44 + 2).Take(40)));
    }

    return result;
}
于 2020-09-09T22:44:42.787 回答