使用 MyString.Remove(x,y) 从 c# 中的字符串中删除多个字符
Removing multiple characters from a string in c# using MyString.Remove(x,y)
虽然 运行 我的服务器中有一个程序(一个 beacon detection 代码),我将收到一个可变大小的字符串, 名为 io385.
字符串长度可以根据检测到的信标数量而变化:
- 一个信标:46字节(前4个字节无用,后40个重要,后2个无用);
- 两个信标:90 字节(前 2 个字节无用,后 40 个 重要,后 2 个无用);
- 三个信标:134字节(前2个字节无用,后40个重要,后2个无用;
...
所以,有了这个,我的想法是删除所有无用的东西。尽管字符串的大小可能不同,但我总是想删除固定位置的字节(对于第一个信标,first 四个和 last 2;对于下一个信标,第一个两个和最后两个)。
我开始手动删除 2 beacon 字符串上无用的字节。但是,我想优化它以便自动处理字符串是 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);
一种方法是先删除前两个字符,以便所有“信标”在字符串中的长度相同(44 个字符)。现在我们可以创建一个循环,从 0
到 length / 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();
}
如果您想将代码修改为 return 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;
}
给出
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
虽然 运行 我的服务器中有一个程序(一个 beacon detection 代码),我将收到一个可变大小的字符串, 名为 io385.
字符串长度可以根据检测到的信标数量而变化:
- 一个信标:46字节(前4个字节无用,后40个重要,后2个无用);
- 两个信标:90 字节(前 2 个字节无用,后 40 个 重要,后 2 个无用);
- 三个信标:134字节(前2个字节无用,后40个重要,后2个无用;
...
所以,有了这个,我的想法是删除所有无用的东西。尽管字符串的大小可能不同,但我总是想删除固定位置的字节(对于第一个信标,first 四个和 last 2;对于下一个信标,第一个两个和最后两个)。
我开始手动删除 2 beacon 字符串上无用的字节。但是,我想优化它以便自动处理字符串是 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);
一种方法是先删除前两个字符,以便所有“信标”在字符串中的长度相同(44 个字符)。现在我们可以创建一个循环,从 0
到 length / 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();
}
如果您想将代码修改为 return 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;
}
给出
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