0

我今天了解了 Stringbuilder 并且一直在搞乱它,因为它可能是完成我需要做的事情的最简单或最快的方法。

我有一个这样的文本文件:

Zach LCPL Schytt
Bill CPL John
Mark LCPL Simmons
...etc

我使用下面的函数将它从列表框读取到文本框。

StringBuilder^ sb = gcnew StringBuilder();
        Convertor^ form2 = gcnew Convertor();
            for (int i = 0; i < listBox1->Items->Count; i++){
                String^ temp = listBox1->Items[i]->ToString();
                sb->AppendFormat("{0}", temp)->AppendLine();
            }
            form2->textBox1->Text = sb->ToString();
            form2->ShowDialog();

我该如何让它看起来像下面呢?对于每个名字,

dn: CN=Schytt LCPL Zach,DC=Sample,DC=Site
changetype: add
displayName: Schytt.Zach

我调查了insert, 和东西,但不太明白。

4

1 回答 1

1

你在寻找这样的东西吗?

StringBuilder^ sb = gcnew StringBuilder();
for (int i = 0; i < listBox1->Items->Count; i++)
{
    String^ temp = listBox1->Items[i]->ToString();

    // First, separate the input string.
    array<String^>^ strings = temp->Split();
    String^ firstName = strings[0];
    String^ rank = strings[1];
    String^ lastName = strings[2];

    // Then build the output string. (Remember that the C++ compiler 
    // concatenates strings at compile time, so we don't need a plus sign.)
    sb->AppendFormat("dn: CN = {2} {3} {1},DC=Sample,DC=Site{0}"
                     "changetype: add{0}"
                     "displayName: {2}.{1}{0}", 
                     Environment::Newline, //0
                     firstName, //1
                     lastName, //2
                     rank); //3
}

form2->textBox1->Text = sb->ToString();
form2->ShowDialog();
于 2013-01-22T04:46:07.387 回答