Insert into Substring 不在索引处插入
Insert into Substring doesn't insert at index
我有以下代码,旨在遍历并将值添加到字符串值。
从字符串生成器的索引 0 开始的每个其他值 RepeatIndexes
应该始终是波浪形的。以及我在别处拥有的图书馆中特定索引的其他值。只是假装
char InsertThis = indexJustForRepetas[IndexOfIndexes[RepeatTime]];
^--='s 'X'--^
这是实际的 code/loop:
StringBuilder RepeatIndexes = new StringBuilder("");
int ZeroPosition = 0; //for the indexes which will remain blank until the next loop-
int charPosition = 1;
int RepeatTime = 0; //1 thru final --^- 1^
while (NumberHalf > 0) //find and replace the char that was repeated within the string, 'AccountableToRepeats', with something that we can decipher later, during decryption-
{
RepeatIndexes.Insert(ZeroPosition, "~"); //inserting a squiggly line until next loop-
char InsertThis = indexJustForRepetas[IndexOfIndexes[RepeatTime]]; //find char at IndexOfIndexes to add into position-
RepeatIndexes.Insert (charPosition, InsertThis);
RepeatTime =RepeatTime +2;
ZeroPosition = ZeroPosition + 2;
NumberHalf = NumberHalf - 1;
}
波浪线 ('~') 的索引从 0 开始:int ZeroPosition = 0;
字符索引 ("X"),从 1 开始:int charPosition = 1;
但出于某种原因,我得到的输出是:~XXX~~
应该是:~X~X~X
字符串生成器的性质有什么问题吗?或者 insert()
我不明白?
循环看起来应该正确递增,但输出对我来说没有任何意义。
我希望这个问题在适合这项服务的范围内。
编辑:
我认为问题是 charPosition
没有更新。您需要使用与 RepeatTime
和 ZeroPosition
相同的值更新 charPosition
。因此,在 while 循环中添加 charPosition += 2;
。发生的事情是 'X'
总是被插入位置 1(第一个 ~
之后的位置)。
此外,您使用 Insert
和所有这些索引使它过于复杂。 RepeatTime
和 ZeroPosition
具有相同的值,因此您不需要两者。
您可以使用 StringBuilder
的 .Append()
将文本添加到字符串的末尾。我使用了字符串插值 ${var}
并假设 numberHalf = 3
但这应该做你想要的:
StringBuilder repeatIndexes = new StringBuilder();
int numberHalf = 3;
// int repeatTime = 0;
while (numberHalf > 0)
{
// replace below line with your char in array
char insertThis = 'X'; //indexJustForRepetas[IndexOfIndexes[repeatTime]];
repeatIndexes.Append($"~{insertThis}");
// repeatTime += 1 // needed for array call above. +1 or +2 depending on your intention
numberHalf = numberHalf - 1;
}
var outString = repeatIndexes.ToString();
output:
~X~X~X
如果您愿意,可以将 repeatIndexes.Append($"~{insertThis}")
行拆分为 2 行:
repeatIndexes.Append("~");
repeatIndexes.Append(insertThis);
我有以下代码,旨在遍历并将值添加到字符串值。
从字符串生成器的索引 0 开始的每个其他值 RepeatIndexes
应该始终是波浪形的。以及我在别处拥有的图书馆中特定索引的其他值。只是假装
char InsertThis = indexJustForRepetas[IndexOfIndexes[RepeatTime]];
^--='s 'X'--^
这是实际的 code/loop:
StringBuilder RepeatIndexes = new StringBuilder("");
int ZeroPosition = 0; //for the indexes which will remain blank until the next loop-
int charPosition = 1;
int RepeatTime = 0; //1 thru final --^- 1^
while (NumberHalf > 0) //find and replace the char that was repeated within the string, 'AccountableToRepeats', with something that we can decipher later, during decryption-
{
RepeatIndexes.Insert(ZeroPosition, "~"); //inserting a squiggly line until next loop-
char InsertThis = indexJustForRepetas[IndexOfIndexes[RepeatTime]]; //find char at IndexOfIndexes to add into position-
RepeatIndexes.Insert (charPosition, InsertThis);
RepeatTime =RepeatTime +2;
ZeroPosition = ZeroPosition + 2;
NumberHalf = NumberHalf - 1;
}
波浪线 ('~') 的索引从 0 开始:int ZeroPosition = 0;
字符索引 ("X"),从 1 开始:int charPosition = 1;
但出于某种原因,我得到的输出是:~XXX~~
应该是:~X~X~X
字符串生成器的性质有什么问题吗?或者 insert()
我不明白?
循环看起来应该正确递增,但输出对我来说没有任何意义。
我希望这个问题在适合这项服务的范围内。
编辑:
我认为问题是 charPosition
没有更新。您需要使用与 RepeatTime
和 ZeroPosition
相同的值更新 charPosition
。因此,在 while 循环中添加 charPosition += 2;
。发生的事情是 'X'
总是被插入位置 1(第一个 ~
之后的位置)。
此外,您使用 Insert
和所有这些索引使它过于复杂。 RepeatTime
和 ZeroPosition
具有相同的值,因此您不需要两者。
您可以使用 StringBuilder
的 .Append()
将文本添加到字符串的末尾。我使用了字符串插值 ${var}
并假设 numberHalf = 3
但这应该做你想要的:
StringBuilder repeatIndexes = new StringBuilder();
int numberHalf = 3;
// int repeatTime = 0;
while (numberHalf > 0)
{
// replace below line with your char in array
char insertThis = 'X'; //indexJustForRepetas[IndexOfIndexes[repeatTime]];
repeatIndexes.Append($"~{insertThis}");
// repeatTime += 1 // needed for array call above. +1 or +2 depending on your intention
numberHalf = numberHalf - 1;
}
var outString = repeatIndexes.ToString();
output:
~X~X~X
如果您愿意,可以将 repeatIndexes.Append($"~{insertThis}")
行拆分为 2 行:
repeatIndexes.Append("~");
repeatIndexes.Append(insertThis);