我想在一个字符串的随机部分插入随机数量的点(从1到7)而不会破坏布局。插入随机数量的点到最小不同位置的字符串?
这是我当前的代码:
Random rand = new Random();
string[] words = iTemplate.Text.Split(' ');
string result = string.Empty;
for (int i = 0; i < words.Count(); i++)
{
string word = words[i];
if (rand.Next(i, words.Count()) == i)
{
for (int dots = rand.Next(1, 7); dots > 0; dots--)
word += ".";
}
result += word + " ";
}
是否有更有效的还是不错的LINQ的选择呢?
现在,由于它的随机性,可能会出现没有点出现的情况。我通过使用if (rand.Next(i, words.Count()) == i)
缩小了它的范围,但似乎仍然有效,但仍然有些结果只显示有1到3个插入点的地方。
我怎么能保证在这个过程中点至少插入4个不同的地方?
样本数据/结果作为每评论请求:
string template = "Hi, this is a template with several words on it and I want to place random dots on 4 different random places every time I run the function";
结果1:
string result = "Hi, this... is a template with several.. words on it and. I want to place random dots on 4 different random...... places every time I run the function";
结果2:
string result = "Hi, this is a template. with several... words on it and I want to..... place random dots on 4 different random. places every time I run the function";
结果3:
string result = "Hi, this. is a template with... several words on it and I want to place random.. dots on 4 different random....... places every time I run the.. function";
你可以给我们之前/之后的例子你想完成什么,你的要求是相当混乱。 –
@ m-y添加样本数据以及结果每次运行时的样子。 – Guapo