简体   繁体   English

合并文字档名称

[英]Combining text files name

I have a list. 我有一个清单。

List<string> slots = HexStringToGenerateFiles(weaponStorageEntity.Slot);

I'm retrieving the list using foreach loop and did some conversion at the same time. 我正在使用foreach循环检索列表,并同时进行了一些转换。

string slotnumber = "";
string ibuttonslot = "";
foreach (string slot in slots)
{
 slotnumber += slot;
 ibuttonslot = ByteOperation.ReverseString((Convert.ToString(1 << ((Int32.Parse(slotnumber)) - 1), 2).PadLeft(16, '0')));
}

Then, save the output as a name of a textfile. 然后,将输出另存为文本文件的名称。

  CreateFile(String.Format("{0}\\B_{1:X16}.TXT",
                                   userDir,
                                   ibuttonslot,
                                   ));

If I have 3 slots, then my output will have 3 textfiles. 如果我有3个插槽,那么我的输出将有3个文本文件。 However, I would like it to combine it be one textfile only. 但是,我希望将其合并为一个文本文件。 My output is something as shown below. 我的输出如下所示。

B_10000000.TXT B_10000000.TXT

B_01000000.TXT B_01000000.TXT

B_00100000.TXT B_00100000.TXT

My desired output is 我想要的输出是

B_11100000.TXT B_11100000.TXT

If you have your slots numbered just as "1", "2", "3", ... and want to produce a name of the file that would represent "1" in a corresponding slot - so for "1 3" it would be 10100000... then you can use something like this: 如果您的插槽编号分别为“ 1”,“ 2”,“ 3”,...,并希望产生在相应插槽中代表“ 1”的文件的名称,那么对于“ 1 3”会是10100000 ...那么您可以使用类似以下的内容:

var slots = new[] { "1" };
var number = 0; 
foreach (var s in slots)
{
    int slotNumber;
    if (!Int32.TryParse(s, out slotNumber)) continue;

    var slot = (int)Math.Pow(2, slotNumber - 1);
    number |= slot;
}

var fileName = Convert.ToString(number, 2).PadLeft(16, '0');
Console.WriteLine(fileName); //output is 0000000000000101

And then revert this string (in your code it is ByteOperation.ReverseString). 然后还原此字符串(在您的代码中为ByteOperation.ReverseString)。

If you want to combine both file's text then you can use File.ReadAllLines(string) method and combine the output like 如果要合并两个文件的文本,则可以使用File.ReadAllLines(string)方法并合并输出,例如

        List<string> contents = System.IO.File.ReadAllLines(firstfile).ToList();
        contents.AddRange(System.IO.File.ReadAllLines(secondfile));

You can try this for combining names: 您可以尝试以下方法来组合名称:

string in1 = "B_10000000.TXT";
string in2 = "B_01000000.TXT";
string in3 = "B_00100000.TXT";
char[] charsInName = in1.ToCharArray();
for (int i = 0; i < charsInName.Length-1; i++)
{
    if (charsInName[i] != in2[i] && charsInName[i] != in3[i])
        charsInName[i] = charsInName[i];
    else if (charsInName[i] != in2[i])
        charsInName[i] = in2[i];
    else if (charsInName[i] != in3[i])
        charsInName[i] = in3[i];
}
string outPutName = String.Join("", charsInName);// will be B_11100000.TXT

Or can use LINQ: 或者可以使用LINQ:

var result = in1.Select(
    (x, y) => x != in2[y] && x != in3[y] ? x :
    x != in2[y] ? in2[y] : 
    x != in3[y] ? in3[y] : x);
string outPutName = String.Join("", result);

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

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